Full stack software engineer with a flair for creating elegant solutions with the least amount of time.Exceptional problem solver with an aptitude for troubleshooting and the ability to quickly master
Hi, thanks for your comment :) Your code solves the same problem as I'm describing in article, only without using custom hook.
I would definitely also use your implementation but still:
Direct destructing of previous state in setState function could happen to be repeated in many event handlers in your component. So you will need to type ...state for many times in one component.
Extracting that "object merging" to separate custom hook could maybe in shortest possible way look like:
If you import this hook into many of your components, than this way still gives you option to avoid destructing of previous state like ...state as i mentioned above.
Becouse of that it just worth it to have it as custom hook :D
Yea.. aside from it not looking nice to always repeat the same thing, there is the issue of forgetting to spread when doing the setState .. and that results in overriding the entire object and other bugs.
For further actions, you may consider blocking this person and/or reporting abuse
We're a place where coders share, stay up-to-date and grow their careers.
Question from my side, why you are not using something like that :
and whenever yo want to update the state, for example lets update the name field :
even with
const
it will behave the sameHi, thanks for your comment :) Your code solves the same problem as I'm describing in article, only without using custom hook.
I would definitely also use your implementation but still:
setState
function could happen to be repeated in many event handlers in your component. So you will need to type...state
for many times in one component.and than to be used like:
If you import this hook into many of your components, than this way still gives you option to avoid destructing of previous state like
...state
as i mentioned above.Yea.. aside from it not looking nice to always repeat the same thing, there is the issue of forgetting to spread when doing the setState .. and that results in overriding the entire object and other bugs.