我點擊按鈕時渲染一個新窗體。所以基本上我從組件中更改狀態:在狀態變化時不響應重新渲染
假爲真或 空真
然而,令人奇怪的狀態改變後,該組件沒有重新渲染
export default class BoardManager extends React.Component{
constructor(){
super();
this.state = {
newForm : null
};
setTimeout(() => {
this.state = {
newForm : true
};
console.log(this.state.newForm);
},1000);
}
render(){
return(
<div>
Some thing here
{this.state.newForm ? <NewBoardForm />: null}
</div>
)
}
}
非常感謝!
編輯!!!解決方案
export default class BoardManager extends React.Component{
constructor(){
super();
this.state = {
newForm : null
};
}
render(){
return(
<div>
<BoardsTable boards={BoardData.boards}/>
<button onClick={() => {
this.setState({
newForm : true
});
console.log(this.state.newForm);
}}>Add New</button>
<button onClick={() => {
this.setState({
newForm : null
});
console.log(this.state.newForm);
}}>Delete</button>
{this.state.newForm ? <NewBoardForm />: null}
</div>
)
}
}
我覺得在ES6 this.state基本上是this.setState –
'this.state ='是不一樣'this.setState'。 – Taysky
@max_new ES6與這些React特定的方法調用無關。 –