小编DLy*_*ith的帖子

状态自己改变?

我有一个 React 更改处理程序来监视一系列复选框。目标是拥有一个包含所有“已检查”条目的 State 数组。处理程序首先将当前状态复制到本地数组变量。然后它测试是否已检查,并将新条目推送到变量或将其从变量中拼接出来。最后,它将该变量设置回状态,为下一个周期做好准备。

问题是,当本地数组更改时,我的 State 数组会立即自动更改。怎么会发生这种事?!?!

这是我的处理程序代码:

    handleEmpChange(parm1, parm2, event) {
        const {name, value, checked} = event.target
        console.log("name: ",name)
        console.log("value: ",value)
        console.log("checked: ",checked)

        // Add the checked employee into the array (or remove it if unchecked)
        let employeesArray = this.state.assignedTo
        if (checked) {
            console.log("employeesArray - before push: ",employeesArray)
            console.log("this.state.assignedTo - before push: ",this.state.assignedTo)
            console.log("this.state.originalAssignedTo - before push: ",this.state.originalAssignedTo)
            employeesArray.push(value)
            console.log("employeesArray - after push: ",employeesArray)
            console.log("this.state.assignedTo - after push: ",this.state.assignedTo)
            console.log("this.state.originalAssignedTo - after push: ",this.state.originalAssignedTo)
        } else …
Run Code Online (Sandbox Code Playgroud)

arrays state reactjs

2
推荐指数
1
解决办法
104
查看次数

标签 统计

arrays ×1

reactjs ×1

state ×1