Reactjs 如何在每个组件渲染之前更新React中的状态?

Reactjs 如何在每个组件渲染之前更新React中的状态?,reactjs,Reactjs,我有一个React组件,看起来像这样: class MyComp extends Component { constructor(props) { super(props); this.state = { myObject: this.props.myObject, }; this.updateState = this.updateState.bind(this); } comp

我有一个React组件,看起来像这样:

class MyComp extends Component {
    constructor(props) {
        super(props);

        this.state = {
            myObject: this.props.myObject,
        };

        this.updateState = this.updateState.bind(this);
    }

    componentWillMount() {
        this.updateState();
    }

    updateState() {
        // removed for brevity, doing some calculations here
        // based on some data that is stored in Redux and is 
        // passed here as props.

        this.setState({
            myObject: newObject
        });
    }

    render() {
        return (
            // removed for brevity, renders some HTML elements
        );
    }
}

MyComp.propTypes = {
    myObject: PropTypes.object.isRequired,
    // some other props
};

export default MyComp;
关键是我也在使用Redux,当在渲染组件上单击某些按钮时,我会更新Redux中的一些状态,并且更新工作正常。然后基于更新的Redux状态,我想更新
mycop
组件的状态,它最初将状态作为一个道具获取,尽管该对象在Redux中没有维护,而是另一个组件的状态,并作为道具传递给
mycop

我只想在每次渲染上述组件之前更新状态。但这似乎不起作用。虽然我在Redux中的数据已经更新并且工作正常,但是像这样调用
componentWillMount
来更新我的本地状态似乎不起作用。我所观察到的是,它只调用了我的
updateState
函数一个,而不是每次重新渲染都调用它。您知道如何在每次渲染调用之前更新状态吗?

您可以使用:

componentWillReceiveProps(nextProps)
React不调用
组件将在安装期间使用初始道具接收道具。它只在组件的某些道具可能更新时调用此方法。调用this.setState通常不会触发componentWillReceiveProps

因此,您可以使用
forceUpdate()
重新呈现html

您可以在此处获得更多详细信息:


您可以使用:

componentWillReceiveProps(nextProps)
React不调用
组件将在安装期间使用初始道具接收道具。它只在组件的某些道具可能更新时调用此方法。调用this.setState通常不会触发componentWillReceiveProps

因此,您可以使用
forceUpdate()
重新呈现html

您可以在此处获得更多详细信息:



这背后的原因是什么?你想通过这样做获得什么?@VivekDoshi我只想在每次调用render方法之前更新我的状态。但是我想,
componentWillMount
只调用一次,而不是在每次渲染组件之前?因为我观察到我的
updateState
函数只被调用了一次。这背后的原因是什么?你想通过这样做获得什么?@VivekDoshi我只想在每次调用render方法之前更新我的状态。但是我想,
componentWillMount
只调用一次,而不是在每次渲染组件之前?因为我观察到我的
updateState
函数只被调用一次。我不认为
forceUpdate
对我来说是正确的,因为我的组件会根据需要经常重新渲染,问题是我的状态不会在每次重新渲染时更新,而
componentWillMount
函数中的my
updateState
函数不会在每次重新渲染时调用。将尝试使用
组件WillReceiveProps
。请为此创建一个plunkr?所以我可以帮你更多我想我没有解释清楚。请注意我的
updateState
函数,它正在进行一些计算,最终结果是更新我的状态。现在,我想要的是每次重新渲染组件(包括初始渲染)时调用
updateState
函数。在我当前的表单中,在初始呈现中调用了
componentWillMount
,因此在初始呈现中调用了我的
updateState
函数。但在所有其他后续重新渲染中,
componentWillMount
未被调用,因此我的
updateState
函数未被调用。@打字错误,如果您需要在每次重新渲染时更新状态,您不应该真正使用状态。但您不应该这样做,因为updateState正在设置状态,因此它将重新渲染视图,如果在每次重新渲染时调用updateState,它将进入无限循环,尽管简单的解决方案是在返回之前在render()内部调用updateState direct。我不认为
forceUpdate
对我来说是正确的,因为我的组件会根据需要经常重新渲染,问题是我的状态不会在每次重新渲染时更新,而
componentWillMount
函数中的my
updateState
函数不会在每次重新渲染时调用。将尝试使用
组件WillReceiveProps
。请为此创建一个plunkr?所以我可以帮你更多我想我没有解释清楚。请注意我的
updateState
函数,它正在进行一些计算,最终结果是更新我的状态。现在,我想要的是每次重新渲染组件(包括初始渲染)时调用
updateState
函数。在我当前的表单中,在初始呈现中调用了
componentWillMount
,因此在初始呈现中调用了我的
updateState
函数。但在所有其他后续重新渲染中,
componentWillMount
未被调用,因此我的
updateState
函数未被调用。@打字错误,如果您需要在每次重新渲染时更新状态,您不应该真正使用状态。但您不应该这样做,因为updateState正在设置状态,因此它将重新渲染视图,若在每次重新渲染时调用UpdateEstate,它将进入无限循环,尽管简单的解决方案是在返回之前在render()内部调用UpdateEstate direct。