本地国家未更新Redux状态更新

问题描述 投票:3回答:1

嗨,我有一个组件,从redux状态更新本地状态

constructor(props){
        super(props);
        this.updatePanelInTemplate = this.updatePanelInTemplate.bind(this);
        this.getPanels = this.getPanels.bind(this);
        this.state = {
            myCustomTemplate: this.props.myCustomTemplate
        }
        console.log(this.state);
    }

const mapStateToProps = (state) => {
    console.log(state);
    return {
        resumeTemplate: state.resume.resumeTemplate,
        components: state.resume.components,
        myCustomTemplate: state.resume.myCustomTemplate
    }
}

这些是我所涉及的代码部分。我正在生命周期钩componentDidUpdate()更新redux状态

我的Redux正在很好地展示变化。我在每一个中都放了一个控制台。它按以下顺序打印

console--mapState // empty state
console--constructor // empty state
console--mapState// full state after update

但是我的本地州在此更新后没有反映任何变化。我不知道为什么。任何更多要求请提及。

reactjs redux
1个回答
2
投票

试试这个

constructor(props){
    super(props);
    this.updatePanelInTemplate = this.updatePanelInTemplate.bind(this);
    this.getPanels = this.getPanels.bind(this);
    this.state = {
        myCustomTemplate: this.props.myCustomTemplate
    }
    console.log(this.state);
}

componentWillReceiveProps(nextProps) {
    const { myCustomTemplate } = nextProps;
    if (myCustomTemplate !== this.state.myCustomTemplate) {
        this.setState({
            ...this.state,
            myCustomTemplate,
        }, () => {
            console.log(this.state);
        })
    }
}

const mapStateToProps = (state) => {
    console.log(state);
    return {
        resumeTemplate: state.resume.resumeTemplate,
        components: state.resume.components,
        myCustomTemplate: state.resume.myCustomTemplate
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.