Learn-React-In-30-Minutes
Learn-React-In-30-Minutes copied to clipboard
Pass todo by reference
Pass the todo object by reference instead of passing its id.
@WebDevSimplified Hi there! I'm simply trying to open a small discussion related the use of React.useState, because in the beginning it seemed weird that I have to create a new array, which for the sake of simplifying usage seems logic now after reading in the documentation of React.useState.
However the references inside the array seems to be the same as the original ones and instead of looking for the object we need to change in the newly copied array, we can simply make the change on the reference we already hold.
It would be nice to hear from you on the changes from the PR so that hopefully I may understand better how React.useState works.
@bogdanudrescu
It looks like what you are trying to do is to directly mutate the state, which is considered bad practice, and can cause some unwanted side-effects, and this is why he is creating a new array and modifying that one instead.
I believe that you should look into something like https://www.npmjs.com/package/use-immer if you want to mutate state directly,
Also take a look at https://immerjs.github.io/immer/ to understand the logic behind using Immer and mutating state.