JavaScript to Know for React

Photo by Benjamin Voros

What JavaScript features you should be familiar with when learning and using React

Current Available Translations:

One of the things I love most about React compared to other frameworks that I've used is how exposed you are to JavaScript when you're using it. There's no template DSL (JSX compiles to sensible JavaScript), the component API has only gotten simpler with the addition of React Hooks, and the framework offers you very little abstraction outside the core UI concerns it's intended to solve.

Because of this, learning JavaScript features is really advisable for you to be effective building applications with React. So here are a few JavaScript features I'd recommend you spend some time learning so you can be as effective as possible working with React.

Template Literals

Template literals are like regular strings with super-powers:

1 const greeting = 'Hello' 2 const subject = 'World' 3 console . log ( ` ${ greeting } ${ subject } ! ` ) 4 5 6 console . log ( greeting + ' ' + subject + '!' ) 7 8 9 function Box ( { className , ... props } ) { 10 return < div className = { ` box ${ className } ` } { ... props } /> 11 }

MDN: Template Literals

Shorthand property names

This is so common and useful that I do this without thinking now.

1 const a = 'hello' 2 const b = 42 3 const c = { d : [ true , false ] } 4 console . log ( { a , b , c } ) 5 6 7 console . log ( { a : a , b : b , c : c } ) 8 9 10 function Counter ( { initialCount , step } ) { 11 const [ count , setCount ] = useCounter ( { initialCount , step } ) 12 return < button onClick = { setCount } > { count } </ button > 13 }

MDN: Object initializer New notations in ECMAScript 2015

Arrow functions

Arrow functions are another way to write functions in JavaScript, but they do have a few semantic differences. Luckily for us in React land, we don't have to worry about this as much if we're using hooks in our project (rather than classes), but the arrow function allows for terser anonymous functions and implicit returns, so you'll see and want to use arrow functions plenty.

1 const getFive = ( ) => 5 2 const addFive = a => a + 5 3 const divide = ( a , b ) => a / b 4 5 6 function getFive ( ) { 7 return 5 8 } 9 function addFive ( a ) { 10 return a + 5 11 } 12 function divide ( a , b ) { 13 return a / b 14 } 15 16 17 function TeddyBearList ( { teddyBears } ) { 18 return ( 19 < ul > 20 { teddyBears . map ( teddyBear => ( 21 < li key = { teddyBear . id } > 22 < span > { teddyBear . name } </ span > 23 </ li > 24 ) ) } 25 </ ul > 26 ) 27 }

One thing to note about the example above is the opening and closing parentheses ( . This is a common way to leverage the arrow function's implicit return capabilities when working with JSX.

MDN: Arrow Functions

Destructuring

Destructuring is probably my favorite JavaScript feature. I destructure objects and arrays all the time (and if you're using useState you probably are too, like so). I love how declarative it is.

1 2 3 4 function makeCalculation ( { x , y : d , z = 4 } ) { 5 return Math . floor ( ( x + d + z ) / 3 ) 6 } 7 8 9 function makeCalculation ( obj ) { 10 const { x , y : d , z = 4 } = obj 11 return Math . floor ( ( x + d + z ) / 3 ) 12 } 13 14 15 function makeCalculation ( obj ) { 16 const x = obj . x 17 const d = obj . y 18 const z = obj . z === undefined ? 4 : obj . z 19 return Math . floor ( ( x + d + z ) / 3 ) 20 } 21 22 23 function UserGitHubImg ( { username = 'ghost' , ... props } ) { 24 return < img src = { ` https://github.com/ ${ username } .png ` } { ... props } /> 25 }

MDN: Destructuring assignment

Definitely read that MDN article. You are certain to learn something new. When you're done, try to refactor this to use a single line of destructuring:

1 function nestedArrayAndObject ( ) { 2 3 const info = { 4 title : 'Once Upon a Time' , 5 protagonist : { 6 name : 'Emma Swan' , 7 enemies : [ 8 { name : 'Regina Mills' , title : 'Evil Queen' } , 9 { name : 'Cora Mills' , title : 'Queen of Hearts' } , 10 { name : 'Peter Pan' , title : ` The boy who wouldn't grow up ` } , 11 { name : 'Zelena' , title : 'The Wicked Witch' } , 12 ] , 13 } , 14 } 15 16 const title = info . title 17 const protagonistName = info . protagonist . name 18 const enemy = info . protagonist . enemies [ 3 ] 19 const enemyTitle = enemy . title 20 const enemyName = enemy . name 21 return ` ${ enemyName } ( ${ enemyTitle } ) is an enemy to ${ protagonistName } in " ${ title } " ` 22 }

Parameter defaults

This is another feature I use all the time. It's a really powerful way to declaratively express default values for your functions.

1 2 3 function add ( a , b = 0 ) { 4 return a + b 5 } 6 7 8 const add = ( a , b = 0 ) => a + b 9 10 11 function add ( a , b ) { 12 b = b === undefined ? 0 : b 13 return a + b 14 } 15 16 17 function useLocalStorageState ( { 18 key , 19 initialValue , 20 serialize = v => v , 21 deserialize = v => v , 22 } ) { 23 const [ state , setState ] = React . useState ( 24 ( ) => deserialize ( window . localStorage . getItem ( key ) ) || initialValue , 25 ) 26 27 const serializedState = serialize ( state ) 28 React . useEffect ( ( ) => { 29 window . localStorage . setItem ( key , serializedState ) 30 } , [ key , serializedState ] ) 31 32 return [ state , setState ] 33 }

MDN: Default parameters

The ... syntax can be thought of as kind of a "collection" syntax where it operates on a collection of values. I use it all the time and strongly recommend you learn how and where it can be used as well. It actually takes different meanings in different contexts, so learning the nuances there will help you.

1 const arr = [ 5 , 6 , 8 , 4 , 9 ] 2 Math . max ( ... arr ) 3 4 Math . max . apply ( null , arr ) 5 6 const obj1 = { 7 a : 'a from obj1' , 8 b : 'b from obj1' , 9 c : 'c from obj1' , 10 d : { 11 e : 'e from obj1' , 12 f : 'f from obj1' , 13 } , 14 } 15 const obj2 = { 16 b : 'b from obj2' , 17 c : 'c from obj2' , 18 d : { 19 g : 'g from obj2' , 20 h : 'g from obj2' , 21 } , 22 } 23 console . log ( { ... obj1 , ... obj2 } ) 24 25 console . log ( Object . assign ( { } , obj1 , obj2 ) ) 26 27 function add ( first , ... rest ) { 28 return rest . reduce ( ( sum , next ) => sum + next , first ) 29 } 30 31 function add ( ) { 32 const first = arguments [ 0 ] 33 const rest = Array . from ( arguments ) . slice ( 1 ) 34 return rest . reduce ( ( sum , next ) => sum + next , first ) 35 } 36 37 38 function Box ( { className , ... restOfTheProps } ) { 39 const defaultProps = { 40 className : ` box ${ className } ` , 41 children : 'Empty box' , 42 } 43 return < div { ... defaultProps } { ... restOfTheProps } /> 44 }

MDN: Spread syntax

MDN: Rest parameters

ESModules

If you're building an app with modern tools, chances are it supports modules, it's a good idea to learn how the syntax works because any application of even trivial size will likely need to make use of modules for code reuse and organization.

1 export default function add ( a , b ) { 2 return a + b 3 } 4 5 6 7 8 9 10 export const foo = 'bar' 11 12 13 14 15 16 17 export function subtract ( a , b ) { 18 return a - b 19 } 20 21 export const now = new Date ( ) 22 23 24 25 26 27 28 29 30 import ( './some-module' ) . then ( 31 allModuleExports => { 32 33 34 35 36 } , 37 error => { 38 39 40 } , 41 ) 42 43 44 import React , { Suspense , Fragment } from 'react' 45 46 47 const BigComponent = React . lazy ( ( ) => import ( './big-component' ) ) 48

MDN: import

MDN: export

As another resource, I gave a whole talk about this syntax and you can watch that talk here

Ternaries

I love ternaries. They're beautifully declarative. Especially in JSX.

1 const message = bottle . fullOfSoda 2 ? 'The bottle has soda!' 3 : 'The bottle may not have soda :-(' 4 5 6 let message 7 if ( bottle . fullOfSoda ) { 8 message = 'The bottle has soda!' 9 } else { 10 message = 'The bottle may not have soda :-(' 11 } 12 13 14 function TeddyBearList ( { teddyBears } ) { 15 return ( 16 < React.Fragment > 17 { teddyBears . length ? ( 18 < ul > 19 { teddyBears . map ( teddyBear => ( 20 < li key = { teddyBear . id } > 21 < span > { teddyBear . name } </ span > 22 </ li > 23 ) ) } 24 </ ul > 25 ) : ( 26 < div > There are no teddy bears . The sadness . </ div > 27 ) } 28 </ React.Fragment > 29 ) 30 }

I realize that ternaries can get a knee-jerk reaction of disgust from some people who had to endure trying to make sense of ternaries before prettier came along and cleaned up our code. If you're not using prettier already, I strongly advise that you do. Prettier will make your ternaries much easier to read.

MDN: Conditional (ternary) operator

Array Methods

Arrays are fantastic and I use array methods all the time! I probably use the following methods the most frequently:

find

some

every

includes

map

filter

reduce

Here are some examples:

1 const dogs = [ 2 { 3 id : 'dog-1' , 4 name : 'Poodle' , 5 temperament : [ 6 'Intelligent' , 7 'Active' , 8 'Alert' , 9 'Faithful' , 10 'Trainable' , 11 'Instinctual' , 12 ] , 13 } , 14 { 15 id : 'dog-2' , 16 name : 'Bernese Mountain Dog' , 17 temperament : [ 'Affectionate' , 'Intelligent' , 'Loyal' , 'Faithful' ] , 18 } , 19 { 20 id : 'dog-3' , 21 name : 'Labrador Retriever' , 22 temperament : [ 23 'Intelligent' , 24 'Even Tempered' , 25 'Kind' , 26 'Agile' , 27 'Outgoing' , 28 'Trusting' , 29 'Gentle' , 30 ] , 31 } , 32 ] 33 34 dogs . find ( dog => dog . name === 'Bernese Mountain Dog' ) 35 36 37 dogs . some ( dog => dog . temperament . includes ( 'Aggressive' ) ) 38 39 40 dogs . some ( dog => dog . temperament . includes ( 'Trusting' ) ) 41 42 43 dogs . every ( dog => dog . temperament . includes ( 'Trusting' ) ) 44 45 46 dogs . every ( dog => dog . temperament . includes ( 'Intelligent' ) ) 47 48 49 dogs . map ( dog => dog . name ) 50 51 52 dogs . filter ( dog => dog . temperament . includes ( 'Faithful' ) ) 53 54 55 dogs . reduce ( ( allTemperaments , dog ) => { 56 return [ ... allTemperaments , ... dog . temperaments ] 57 } , [ ] ) 58 59 60 61 function RepositoryList ( { repositories , owner } ) { 62 return ( 63 < ul > 64 { repositories 65 . filter ( repo => repo . owner === owner ) 66 . map ( repo => ( 67 < li key = { repo . id } > { repo . name } </ li > 68 ) ) } 69 </ ul > 70 ) 71 }

MDN: Array

Nullish coalescing operator

If a value is null or undefined , then you may want to fallback to some default value:

1 2 x = x || 'some default' 3 4 5 6 7 add ( null , 3 ) 8 9 10 function add ( a , b ) { 11 a = a == null ? 0 : a 12 b = b == null ? 0 : b 13 return a + b 14 } 15 16 17 function add ( a , b ) { 18 a = a ? ? 0 19 b = b ? ? 0 20 return a + b 21 } 22 23 24 function DisplayContactName ( { contact } ) { 25 return < div > { contact . name ? ? 'Unknown' } < / div > 26 }

MDN: Nullish coalescing operator

Optional chaining

Also known as the "Elvis Operator," this allows you to safely access properties and call functions that may or may not exist. Before optional chaining, we used a hacky-workaround that relied on falsy/truthy-ness.

1 2 const streetName = user && user . address && user . address . street . name 3 4 5 const streetName = user ? . address ? . street ? . name 6 7 8 const onSuccess = options ? . onSuccess 9 10 11 onSuccess ? . ( { data : 'yay' } ) 12 13 14 options ? . onSuccess ? . ( { data : 'yay' } ) 15 16 17 18 19 options ? . onSuccess ( { data : 'yay' } ) 20 21 22 function UserProfile ( { user } ) { 23 return ( 24 < div > 25 < h1 > { user . name } < / h1 > 26 < strong > { user . bio ? . slice ( 0 , 50 ) } ... < / strong > 27 < / div > 28 ) 29 }

A word of caution around this is that if you find yourself doing ?. a lot in your code, you might want to consider the place where those values originate and make sure they consistently return the values they should.

MDN: Optional chaining

Promises and async/await

This one's a big subject and it can take a bit of practice and time working with them to get good at them. Promises are everywhere in the JavaScript ecosystem and thanks to how entrenched React is in that ecosystem, they're everywhere there as well (in fact, React itself uses promises internally).

Promises help you manage asynchronous code and are returned from many DOM APIs as well as third party libraries. Async/await syntax is a special syntax for dealing with promises. The two go hand-in-hand.

1 function promises ( ) { 2 const successfulPromise = timeout ( 100 ) . then ( result => ` success: ${ result } ` ) 3 4 const failingPromise = timeout ( 200 , true ) . then ( null , error => 5 Promise . reject ( ` failure: ${ error } ` ) , 6 ) 7 8 const recoveredPromise = timeout ( 300 , true ) . then ( null , error => 9 Promise . resolve ( ` failed and recovered: ${ error } ` ) , 10 ) 11 12 successfulPromise . then ( log , logError ) 13 failingPromise . then ( log , logError ) 14 recoveredPromise . then ( log , logError ) 15 } 16 17 function asyncAwaits ( ) { 18 async function successfulAsyncAwait ( ) { 19 const result = await timeout ( 100 ) 20 return ` success: ${ result } ` 21 } 22 23 async function failedAsyncAwait ( ) { 24 const result = await timeout ( 200 , true ) 25 return ` failed: ${ result } ` 26 } 27 28 async function recoveredAsyncAwait ( ) { 29 let result 30 try { 31 result = await timeout ( 300 , true ) 32 return ` failed: ${ result } ` 33 } catch ( error ) { 34 return ` failed and recovered: ${ error } ` 35 } 36 } 37 38 successfulAsyncAwait ( ) . then ( log , logError ) 39 failedAsyncAwait ( ) . then ( log , logError ) 40 recoveredAsyncAwait ( ) . then ( log , logError ) 41 } 42 43 function log ( ... args ) { 44 console . log ( ... args ) 45 } 46 47 function logError ( ... args ) { 48 console . error ( ... args ) 49 } 50 51 52 function timeout ( duration = 0 , shouldReject = false ) { 53 return new Promise ( ( resolve , reject ) => { 54 setTimeout ( ( ) => { 55 if ( shouldReject ) { 56 reject ( ` rejected after ${ duration } ms ` ) 57 } else { 58 resolve ( ` resolved after ${ duration } ms ` ) 59 } 60 } , duration ) 61 } ) 62 } 63 64 65 function GetGreetingForSubject ( { subject } ) { 66 const [ isLoading , setIsLoading ] = React . useState ( false ) 67 const [ error , setError ] = React . useState ( null ) 68 const [ greeting , setGreeting ] = React . useState ( null ) 69 70 React . useEffect ( ( ) => { 71 async function fetchGreeting ( ) { 72 try { 73 const response = await window . fetch ( 'https://example.com/api/greeting' ) 74 const data = await response . json ( ) 75 setGreeting ( data . greeting ) 76 } catch ( error ) { 77 setError ( error ) 78 } finally { 79 setIsLoading ( false ) 80 } 81 } 82 setIsLoading ( true ) 83 fetchGreeting ( ) 84 } , [ ] ) 85 86 return isLoading ? ( 87 'loading...' 88 ) : error ? ( 89 'ERROR!' 90 ) : greeting ? ( 91 < div > 92 { greeting } { subject } 93 </ div > 94 ) : null 95 }

MDN: Promise

MDN: async function

MDN: await

Conclusion

There are of course many language features which are useful when building React apps, but these are some of my favorites that I find myself using again and again. I hope you find this useful.

If you'd like to dive into any of these further, I do have a JavaScript workshop which I gave and recorded while I was working at PayPal which you may find helpful: ES6 and Beyond Workshop at PayPal

Good luck!