I have a page for listing products in a table, each row of the table has checkbox who meant to toggle the row’s active (Boolean)
state when it clicked. The problem is that after updating the state the DOM doesn’t change.
This how my code looks like:
const [products, setProducts] = useState([]);
useEffect(() => {
fetchProducts(); //fetch the products from the server
}, []);
const productChecked = (index) => {
setProducts((prevState) => {
const newState = prevState;
newState[index].active = !newState[index].active;
return newState;
});
};
...
{products.map((product, index) => {
...
<td>
<input type="checkbox" checked={!product.active} onClick={() => productChecked(index)}/>
</td>
...
})}
I added this useEffect
to test it, but it doesn’t seem to work at all (I can’t see this console log):
useEffect(() => {
console.log('products',products);
}, [products]);
I can’t understand why it is happening… what am I missing here?
Thanks in advance 🙂
3
Answers
In your code, you are trying to assign prev array to new array which is not changing the reference thus not re-rendering the component.
So in order to re render it you’ll have to change the reference
You can also try this approach this will reduce the dependency on callback and prevState.
};
When you want to update the state and trigger a rerender, you should create a new object or array that represents the updated state, rather than modifying the existing state object directly.