我實際上正在學習reactjs,而我實際上正在開發一個包含在名爲TODO的「父組件」內的小TODO列表。Reactjs,父組件,狀態和道具
在這個父類的內部,我想從關注的存儲中獲取TODO的當前狀態,然後將此狀態作爲屬性傳遞給子組件。
問題是,我不知道在哪裏初始化我的父狀態值。其實,我使用ES6語法,所以我沒有getInitialState()函數。它寫在文檔中,我應該使用組件構造函數來初始化這些狀態值。
事實是,如果我想初始化我的構造函數內部的狀態,this.context(Fluxible Context)實際上是未定義的。
我決定移動componentDidMount中的初始化,但它似乎是反模式,我需要另一種解決方案。你可以幫我嗎 ?
這裏是我的實際代碼:
import React from 'react';
import TodoTable from './TodoTable';
import ListStore from '../stores/ListStore';
class Todo extends React.Component {
constructor(props){
super(props);
this.state = {listItem:[]};
this._onStoreChange = this._onStoreChange.bind(this);
}
static contextTypes = {
executeAction: React.PropTypes.func.isRequired,
getStore: React.PropTypes.func.isRequired
};
componentDidMount() {
this.setState(this.getStoreState()); // this is what I need to move inside of the constructor
this.context.getStore(ListStore).addChangeListener(this._onStoreChange);
}
componentWillUnmount() {
this.context.getStore(ListStore).removeChangeListener(this._onStoreChange);
}
_onStoreChange() {
this.setState(this.getStoreState());
}
getStoreState() {
return {
listItem: this.context.getStore(ListStore).getItems() // gives undefined
}
}
add(e){
this.context.executeAction(function (actionContext, payload, done) {
actionContext.dispatch('ADD_ITEM', {name:'toto', key:new Date().getTime()});
});
}
render() {
return (
<div>
<button className='waves-effect waves-light btn' onClick={this.add.bind(this)}>Add</button>
<TodoTable listItems={this.state.listItem}></TodoTable>
</div>
);
}
}
export default Todo;
我很好奇,你在哪裏讀到,在componentDidMount方法中獲取或設置'state'是一個_anti-pattern_? –
@TahirAhmed,在這裏:https://facebook.github.io/react/tips/props-in-getInitialState-as-anti-pattern.html –
hmm但它在那篇文章中提到了'componentDidMount'?我的意思是我在我的項目中所做的幾乎與你正在做的一樣,即在'componentDidMount'函數內使用'this.setState(...)',直到現在,我認爲使用它就好了。我現在很好奇別人怎麼看,因爲我個人認爲沒有任何問題。 –