Check out bidbear.io Amazon Advertising for Humans. Now publicly available ๐Ÿš€

Writing Stable JavaScript and React

Intro

There is nothing more grating than having your application crash on users because of errors. And while they arenโ€™t totally avoidable, letโ€™s put together some best practices here that we can follow to reduce these as much as possible.

Try/Catch

๐Ÿ“˜ MDN: Try Catch

Try/catch/finally blocks are well known, but letโ€™s just discuss here when we should or should not be using them.

Try catch blocks can make it more difficult to debug where an error is occurring, and as such should not be used totally indiscriminantly (in every function). However there are some times where we absolutely should be using them.

  • Anything involving API queries
  • Anything involving user input
  • When using async/await, try-catch blocks are the standard way to handle asynchronous errors.

Really when it is anytime that we donโ€™t have absolute control over our inputs.

Safe Navigation Operator

Use optional chaining (?.) and nullish coalescing (??) to navigate safely through objects.

Optional Chaining

One of the best ways to prevent type errors trying to access properties or values in objects or arrays is optional chaining. Take for example the following:

let someObject = {};
let someVariable = someObject.someProperty.someNestedProperty 
// โš  TypeError: someObject.someProperty is not defined

To prevent this extremely common scenario from throwing an error we can use optional chaining.

๐Ÿ“˜ MDN: Optional Chaining ๐Ÿ“˜ GeeksForGeeks: Optional Chaining

Which if used correctly would look like this

let someObject = {};
let someVariable = someObject?.someProperty?.someNestedProperty 
console.log(someVariable); // undefined 

We are now returning undefined, and no error.

Optional chaining wonโ€™t help us in the following scenario, where the object someObject itself is not defined.

// notable absence of someObject declaration here...

let someVariable = someObject?.someProperty?.someOtherProperty
// โš  Uncaught ReferenceError: someObject is not defined

The best part is that there is no downside to just using this all the time while trying to access nested methods or variables. Anytime you are trying to access nested items just use the question marks.

Nullish coalescing

Nullish coalescing is an easy way to provide a default value to a variable.

const foo = null ?? 'default string';
console.log(foo);
// Expected output: "default string"

const baz = 0 ?? 42;
console.log(baz);
// Expected output: 0

If the value on the left side of the operator is null or undefined then it will return the value on the right side, typically some type of default value.

๐Ÿ“˜ MDN: Nullish Coalescing Operator

This can be combined with optional chaining to prevent errors while accessing nullish properties and providing a default value.

const foo = { someFooProp: "hi" };

console.log(foo.someFooProp?.toUpperCase() ?? "not available"); // "HI"
console.log(foo.someBarProp?.toUpperCase() ?? "not available"); // "not available"

React Error Boundaries

React error boundaries prevent the entire application from crashing if there are uncaught errors, and can display a fallback ui.

๐Ÿ“˜ React Docs: Error Boundaries

this can be simplified with a package/component:

๐Ÿ“˜ NPM: react-error-boundary

and depending on whether you have an error reporting tool installed, they may have their own error boundary component:

Sentry: React Error Boundary

Amazon Ad Analytics For Humans

Advertising reports automatically saved and displayed beautifully for powerful insights.

bidbear.io
portfolios page sunburst chart