React useState bugs series Part II
The white-blank screen in React is a common scenario for many developers and it doesn't only come from the useState hook but rather many errors.
With useState it can also come from many scenarios but today we are going to talk about giving your initial state value a blue- print .
What do I actually mean let's take an example of an application
So basically that's a very simple app with the result on the right side and you can see that I initialez user in the state and in useState brackets I put nothing and if you can see if I try to access the user's name I get an error:
So this means when you initialize a value with useState and put nothing it is considered undefined and JavaScript tolerates many things but not accessing a property of an undefined object.
So let's make a small change and initialize the user as an empty object:
You can certainly see that we still have a blank screen but no error let's add some text in the div
You see it's a small fix and it would be better if you really gave a clear template.
Thanks save yourself from some bugs and frustration.
See you in the next one.
Top comments (0)