1. 程式人生 > >React實踐:自定義html特性不顯示

React實踐:自定義html特性不顯示

clean 框架 user use scenarios () each 後來 方法

發現React中自定義的html特性在render後是不現實,而且getAttribute方法也只能獲取到undefined。

後來去stackoverflow提問,網友回答說:

It depends on which attributes you are talking about. Usually, ReactJS components can handle most of the commons scenarios without the need of self defining attributes. – Hemerson Carlin 52 mins ago

也對,畢竟React本來就是構架UI的框架,似乎也沒必要在html中綁定自定義屬性,data都在組件內部,完全可以創建數據對象來存儲信息。

不過也有另一個網友給出一種在渲染時給元素自定義標識的方法:

1       this.state.list.forEach(function(v,i,a){
2       let id = v.vid.toString();
3        let temp=<li  key={id} onClick={() => that.handle_del(id)}  > {v.thing}</li>;
4         todo.push(temp);
5       });

這裏id其實利用了閉包,在調用組件的方法時,把當前環境下的id傳過去。也還不錯。

React實踐:自定義html特性不顯示