以我反應-Redux的應用,我有以下呈現compoent反應組分:終極版:基於條件
render(){
return(<div style={this.setStyler()} >
{this.props.value}
</div>);
}
setStyler()
的操縱一個減速器,並返回其應css
被應用的值。
的問題是,我面對的錯誤:
Cannot update during an existing state transition (such as within render or another component's constructor). Render methods should be a pure function of props and state; constructor side-effects are an anti-pattern, but can be moved to componentWillMount.
看來,終極版不允許我根據狀態條件下呈現組件。但我不知道我該如何解決這個問題?
更新:
setStyler(){
if(this.props.activeWord!=this.props.wordId)
return { color:'black', fontWeight: 'normal' };
if(this.props.letterId>this.props.activeLetter && this.props.activeWord==this.props.wordId)
return { color:'black', fontWeight: 'normal' };
if(this.props.letterId==this.props.activeLetter && this.props.activeWord==this.props.wordId){
if(this.props.value==this.props.newTypedLetter){
this.props.color({ color:'green', fontWeight: 'bold' }); //change reducer
return { color:'green', fontWeight: 'bold' };
}
if(this.props.value!=this.props.newTypedLetter){
this.props.color({ color:'red', fontWeight: 'bold' }); ////change reducer
return { color:'red', fontWeight: 'bold' };
}
}
if(this.props.letterId<this.props.activeLetter && this.props.activeWord==this.props.wordId)
return this.props.letterColor;
}
幫助提供的信息很困難。添加setStyler()功能代碼 –
您提供的代碼不會導致您看到的錯誤。你還在哪裏使用setState()? –