【问题标题】:Local state in redux is not updatedredux 中的本地状态未更新
【发布时间】:2017-10-27 08:53:35
【问题描述】:

我的班级有状态,更新了两个函数:

export default class ProductList extends Component {
    constructor(props) {
        super(props);
        this.state = {
            productIdListToEdit: [],
        }
    }


handleCheckClick = (elementId, e) => {
  ...
        this.setState({ productIdListToEdit: tempList })
    }


checkAllAction = () => {
  ...
        this.setState({ productIdListToEdit: [1, 2, 3] })
    }
 }
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react-dom.min.js"></script>

这个状态是转移返回类:

 <GroupEditButton
 productIdListToEdit={this.state.productIdListToEdit}   >

在用“handleCheckClick”改变状态后,类内部状态正在改变,但在使用“checkAllAction”后,局部状态仍然改变,但更深层次(在较低的Class-EditButton中)就像一个冻结 - 状态productIdListToEdit没有改变。

export default class EditButton extends Component {
    constructor(props) {
        super(props)
        this.state = {
            productIdListToEdit: this.props.productIdListToEdit,
        }
    }

    render() {
        console.log(this.state.productIdListToEdit)
        if (this.state.productIdListToEdit.length > 0) {
            return (
                ...
            )
        }
    }
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react-dom.min.js"></script>

【问题讨论】:

  • 你说的“但更深的就像一个冰冻的”是什么意思? “没什么”需要改变什么?请添加更多详细信息吗?
  • 在 EditButton 类状态是从上读取。当我点击“checkAllAction”时,状态不会改变——它总是一样的
  • 使用 componentWillReceiveProps 并在每次道具更改时重置状态。

标签: reactjs redux state local


【解决方案1】:

构造函数只在类初始化时被调用一次。要查看更新,请直接使用props:

export default class EditButton extends Component {

    render() {
        console.log(this.props.productIdListToEdit)
        if (this.props.productIdListToEdit.length > 0) {
            return (
                ...
            )
        }
    }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-06-29
    • 2019-05-30
    • 2018-08-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多