redux中的本地状态未更新

时间:2017-10-27 08:53:36

标签: reactjs redux state local

我班上有状态,更新了两个功能:

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”改变状态后,Inside Class状态正在改变,但是在使用“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>

1 个答案:

答案 0 :(得分:0)

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

export default class EditButton extends Component {

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