初始化
第一次 Component 被載入時,會做哪些事?
- 執行 getInitialState
- getInitialState 去 Store 拿 state 第一次初始化需要載入的資料
- 執行 componentWillMount
- 執行 Component render
- 執行 componentDidMount
- componentDidMount 會加入 store 的 addChangeListener
1 | TestStore.addChangeListener(this.storeCallback); |
有值更動時
執行被 bind 的 onChange
1 | <textarea rows="4" cols="50" onChange={this._onChange} value={this.state.text}/> |
onChange 取得值後傳給 action
1 | _onChange: function(event) { |
Action 呼叫 AppDispatcher,並告訴它要做什麼事(ActionTypes),跟把要更新的值傳給它。
1 | module.exports = { |
接著 Store 的 dispatcherIndex 被觸發
1 | dispatcherIndex: AppDispatcher.register(function(payload) { |
emitChange 被呼叫後,會通知所有 listen 這個 Store 的 Component,Component 的 callback 會收到通知。
1 | storeCallback: function() { |
callback 要做的事,就是重新去 Store 拿新的值,並把自己更新(this.setState)。
當 callback 做 setState 的時候,會觸發 shouldComponentUpdate,如果回傳值是 true
會接著執行 componentWillUpdate
然後會重新 render component
render 完後會,會執行 componentDidUpdate
資料來源