【发布时间】:2018-04-05 04:20:59
【问题描述】:
import * as React from 'react';
export default class Parent extends React.Component {
loadData() {
return {
valueA: this.props.intl.formatMessage({id: 'app.placeHolder'}),
valueB: this.getTheValue
};
}
getTheValue(value) {
return this.props.intl.formatMessage({id: 'app.placeHolder'}, {price: value});
}
render() {
return <Child data={this.loadData()} />;
}
}
class Child extends React.Component {
render() {
return <div>{this.props.data.valueB(1000)}</div>;
}
}
我有上面的代码,我想在子组件中使用getTheValue函数。这里我需要在父组件中绑定getTheValue函数。我使用以下2种方式绑定
1) getTheValue = (value) => {
return this.props.someFunc('placeHolder', value);
}
或
2) valueB: () => this.getTheValue()
主要问题是,这两种方式出现以下错误。
[ 'Warning: 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`.' ]
我想知道这个错误是否与绑定有关?我绑定正确吗? 代码可以通过其中一种方式正常工作,但是当我运行测试但通过测试没有任何问题时出现错误。但我想删除错误。
【问题讨论】:
-
您拥有的箭头功能应该正确绑定。该错误意味着您以某种方式在渲染函数中调用状态更新
标签: javascript reactjs ecmascript-6 redux jestjs