大概大半年時間都在用react寫項目,一直在筆記上零零星星地記錄着,在新的一年即將到來之際,打算整理整理髮出來。javascript
class
在 JSX 裏要寫成
className
,由於
class
在 JS 裏是保留關鍵字。同理某些屬性好比
for
要寫成
htmlFor
。
class Counter extends Component { constructor(props) { super(props); this.state = { count: props.initialCount }; } render() { // ... } }
使用 ES6 語法,能夠直接定義 defaultProps
這個類屬性來替代,這樣能更直觀的知道 default props 是預先定義好的對象值:html
Counter.defaultProps = { initialCount: 0 };
(1)componentWillMount
render
以前調用,你能夠在這個方法裏面調用
setState
改變狀態,而且不會致使額外調用一次
render
(2)componentDidMount
render
以後調用,從這裏開始能夠經過
ReactDOM.findDOMNode(this)
獲取到組件的 DOM 節點。
這些方法不會在首次 render
組件的週期調用java
componentWillReceiveProps
shouldComponentUpdate
componentWillUpdate
componentDidUpdate
statics
對象容許你定義靜態的方法,這些靜態的方法能夠在組件類上調用;在這個塊兒裏面定義的方法都是靜態的,意味着你能夠在任何組件實例建立以前調用它們,這些方法不能獲取組件的 props 和 state。若是你想在靜態方法中檢查 props 的值,在調用處把 props 做爲參數傳入到靜態方法。
表單組件能夠經過 onChange
回調函數來監聽組件變化。當用戶作出如下交互時,onChange
執行並經過瀏覽器作出響應:react
<input>
或 <textarea>
的 value
發生變化時。<input>
的 checked
狀態改變時。<option>
的 selected
狀態改變時。radio
、
checkbox
的
<input>
支持
defaultChecked
屬性,
<select>
支持
defaultValue
屬性。
this.props.key
from a component (eg. the render function) is not defined. If you need to access the same value within the child component, you should pass it as a different prop (ex: <ListItemWrapper key={result.id} id={result.id} />
).
ref
and key
) which are used by React, and are thus not forwarded to the component
<i style={{float:'right',marginRight:'7px'}} data-tip={__('僅供查詢報警對象名稱')} place="top" className="iconfont tips"></i>
即一些自定義的元素,最好使用 data- 放在開頭。git
好了,本人不才,先到此爲止。web