I have code that toggles the value on click from an object in an array. But the console log always shows 1 0 (doubles the values)….
Can please tell what the issue could be?
const { useState } = React;
const App = () => {
const [Btns, setBtns] = useState([
{ componentId: 0 },
{ componentId: 0 }
]);
return (
<div>
<button
onClick={(event) => {
setBtns((oldArr) => {
let newArr = [...oldArr];
newArr[0].componentId = newArr[0].componentId === 0 ? 1 : 0;
console.log(newArr[0].componentId);
return newArr;
});
}}
>
Button
</button>
</div>
);
};
ReactDOM.render(<App />, document.getElementById("react"));
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/17.0.1/umd/react.production.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react-dom/17.0.1/umd/react-dom.production.min.js"></script>
<div id="react"></div>
2
Answers
It is most-likely because you have your
<App>
component wrapped in<StrictMode>
in your sandbox.File:
index.js
Try removing the
<StrictMode>
tags and try to see if that helps.Note: Your sandbox uses strict mode, but your provided snippet does not.
To understand how strict mode differs from non-strict mode, see:
Use immutable state changes
When updating state in React, it is best to not mutate the previous state. State should be treated as immutable. If you need to change a nested property, you should clone i.e.
structuredClone
the current state, before modification.I think this is related to React’s behavior in Strict mode.
The way you are updating the state inside the setState is called an updater function.
In React’s Strict mode, React tends to run the updated functions twice to help detect any hidden bugs.
This is mentioned on React’s official Documentation.
You can Check it here.
However, I think the code used to update the state is somehow faulty.
You should not mutate the state directly by calling newArr[0].componentId
Instead, you can do this:
Check my codesanbox