我有两个React源文件,A
和B
。A
,在其render方法中,调用B
如下:
{this.state.classificationSelected
?
<div>
<B classification={this.props.categories[this.state.categoryIndex].classes[this.state.classificationIndex]} ref="B"/>
</div>
:
null
}
因此B
,如果被渲染Classification
的成分A
被选中。
在中B
,我有以下方法,该方法根据A来计算一些值Classification
:
getSingleChoiceAttributes: function() {
var singleChoiceAttributes = [];
for(let attribute of this.props.classification.attributes){
if(attribute.attributeType == 'SingleChoice') {
singleChoiceAttributes.push(attribute);
}
}
return singleChoiceAttributes;
},
目前,我正在从B的ComponentWillMount
方法中调用此方法。
我希望每次A
更改分类时都执行此方法。但是,目前仅在A
首次选择分类时才调用它。
我怎样才能让getSingleChoiceAttributes
每一次方法改变A
的Classification
(表示为this.state.classificationIndex
)被改变?
void componentWillReceiveProps(
object nextProps
)
当组件正在接收新道具时调用。初始渲染不调用此方法。
以此为契机,通过使用this.setState()更新状态来调用render()之前对prop过渡做出反应。可以通过this.props访问旧的道具。在此函数中调用this.setState()不会触发其他渲染。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句