DEV Community

loading...

Discussion on: 😲🀯The most outstanding new feature in Javascript you need to know about: Optional Chaining

lampewebdev profile image
Michael "lampe" Lazarski Author

Like always thinking is the key here πŸ˜‰

Thread Thread
qm3ster profile image
Mihail Malo

IMO it lends itself to patterns like:

useWidth(this.props.data?.myObject?.width||100)
alsoUseWidth(this.props.data?.myObject?.width||222)

Not only are you doing extra branching, but by having defaults at the usage site instead of defaults site you could have inconsistencies.

Instead, on construction of the this, you could have

this.data=_.merge(defaultData, incomingData)

and then access all the properties unconditionally.
(You could also only merge in properties that already exist in defaultData tree, and perform validation at this stage, creating a very predictable this)