I got this error: "State updates from the useState() and useReducer() Hooks don't support the second callback..." when i make a change in my state
I got this error: "State updates from the useState() and useReducer() Hooks don't support the second callback..." when i make a change in my state
我在标题中粘贴的句子是我从我的代码中得到的,我正在尝试使用挂钩更改数组中的状态,这是我的代码。
export default function card(){
let array = [true,false]
const [change, setChange]=useState(array)
function toggleIcon() {
setChange(
...change,
change[0]=!change[0]
)
console.log(change)
}
return(
</Fragment>
{ change[0] ? (<p>hi</p>): (<p>bye</p>)}
</Fragment>
)
}
我得到了第一个更改,我将 hi 更改为 bye...但是当我再次单击它时,出现了这个错误 MyContracts.js:18
Uncaught TypeError: change is not iterable
你解构错误,请尝试下面的代码,你不必解构,但如果你想尝试,还有注释代码。
function toggleIcon() {
//let newState = [...change];
//newState[0] = !change[0];
//setChange(newState);
change[0]=!change[0]
setChange(change);
console.log(change)
}
我在标题中粘贴的句子是我从我的代码中得到的,我正在尝试使用挂钩更改数组中的状态,这是我的代码。
export default function card(){
let array = [true,false]
const [change, setChange]=useState(array)
function toggleIcon() {
setChange(
...change,
change[0]=!change[0]
)
console.log(change)
}
return(
</Fragment>
{ change[0] ? (<p>hi</p>): (<p>bye</p>)}
</Fragment>
)
}
我得到了第一个更改,我将 hi 更改为 bye...但是当我再次单击它时,出现了这个错误 MyContracts.js:18
Uncaught TypeError: change is not iterable
你解构错误,请尝试下面的代码,你不必解构,但如果你想尝试,还有注释代码。
function toggleIcon() {
//let newState = [...change];
//newState[0] = !change[0];
//setChange(newState);
change[0]=!change[0]
setChange(change);
console.log(change)
}