How to use Optional Chaining and Nullish Coalescing with TypeScript and React

How to use Optional Chaining and Nullish Coalescing with TypeScript and React

Learn how to properly use Optional Chaining and Nullish Coalescing with TypeScript and React. The star of the show in optional chaining is the new (?.), The show’s next star is the nullish coalescing operator (??). Typically with an API call but not limited to. Depending on how deep the property was you had to write some ugly if statements to prevent the error? Well, that’s what we are going to be solving today with Optional Chaining.

Intro

The problem we are trying to solve: Have you ever had a deeply nested property in an object that any time you time to reference it you got back null because it wasn’t defined quick enough before your code ran? Typically with an API call but not limited to. Depending on how deep the property was you had to write some ugly if statements to prevent the error? Well, that’s what we are going to be solving today with Optional Chaining.

Optional Chaining

As briefly discussed in the intro, The star of the show in optional chaining is the new (?.) operator that enables you to capture the property value located deep within a chain of connected objects without checking that each property in the chain is valid.

javascript programming react typescript

Bootstrap 5 Complete Course with Examples

Bootstrap 5 Tutorial - Bootstrap 5 Crash Course for Beginners

Nest.JS Tutorial for Beginners

Hello Vue 3: A First Look at Vue 3 and the Composition API

Building a simple Applications with Vue 3

Deno Crash Course: Explore Deno and Create a full REST API with Deno

How to Build a Real-time Chat App with Deno and WebSockets

Convert HTML to Markdown Online

HTML entity encoder decoder Online

How native is React Native? | React Native vs Native App Development

Article covers: How native is react native?, React Native vs (Ionic, Cordova), Similarities and difference between React Native and Native App Development.

React cloneElement: A Better Way to Build a Component API Props in ES6 Javascript and Ts

Using the React cloneElement is a better way to build a component API props in ES6 Javascript and Typescript. Here's a tutorial on how to do so.

React With Typescript, Redux And Thunk - React Javascript Tutorial

Wanted to make a video with redux and redux-thunk using typescript. I found this a bit confusing when I first learned it and hopefully this videos makes the ...

React With Typescript Crash Course - How To Use React With Typescript

I show how to use React with Typescript: - How to define a component How to specify props using types and interfaces - How to define state - How to handle events

A Quick Intro to TypeScript for JavaScript Programmers

If you are a newer JavaScript developer like me you may have come across the superset of JavaScript known as TypeScript. TypeScript is an open-source extension of JavaScript that changes JavaScript from a functional programming language into an object-oriented programming language.