JavaScript Optional Chaining ?.

Image by PIRO4D from Pixabay

React encourages you to write declarative code. Every now and then you end up writing code that looks like this:

It is a bummer to even write smaller expressions such as

Now for CRA based apps, you can use the new optional chaining feature. First, you must upgrade to version 3.3.0. If applicable, you may also need to upgrade your TypeScript to 3.7+ and probably upgrade VSCode,

Here are the use cases.

Stacking

You can stack the optional chaining operator. The initial examples in this article now become:

The more code there is the more chances of human errors. Declarative style with functional programming patterns and constructs like ‘optional chaining’ help write code that is concise and less error prone.

Frontend Architect, San Jose, CA