Answer the question
In order to leave comments, you need to log in
Is empty object in state instead of null bad practice?
For example, in a container component, you can specify the state as
state = {
someValue: {} // null
}
this.state.someValue.test
. In case of null there will be a natural error
Answer the question
In order to leave comments, you need to log in
Doesn't affect performance. Affects the one who later (perhaps you yourself) will then catch bugs.
undefined - means that the variable is not defined.
null - means "empty".
Accordingly, in the console, when I see undefined, then I will go first of all to check the input parameters of the component / function / etc.
If I see null, then I will go first of all to check overrides / entries.
If you leave an empty object, then it's a little more complicated.
If you need to FIRST get the data, and only then access the object, then it will not be possible to check for an empty object, because the if statement does not compare fields and methods, but checks whether two variables refer to the same object.
const obj = {};
const obj2 = {};
console.log( obj === obj2 ) // false
if( obj === {} ) // false
Is empty object in state instead of null bad practice?
Run typescript, it will tell you where is good and where is bad practice
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question