This site runs best with JavaScript enabled.

JavaScript to Know for React


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:

1const greeting = 'Hello'
2const subject = 'World'
3console.log(`${greeting} ${subject}!`) // Hello World!
4
5// this is the same as:
6console.log(greeting + ' ' + subject + '!')
7
8// in React:
9function 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.

1const a = 'hello'
2const b = 42
3const c = {d: [true, false]}
4console.log({a, b, c})
5
6// this is the same as:
7console.log({a: a, b: b, c: c})
8
9// in React:
10function 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.

1const getFive = () => 5
2const addFive = a => a + 5
3const divide = (a, b) => a / b
4
5// this is the same as:
6function getFive() {
7 return 5
8}
9function addFive(a) {
10 return a + 5
11}
12function divide(a, b) {
13 return a / b
14}
15
16// in React:
17function 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// const obj = {x: 3.6, y: 7.8}
2// makeCalculation(obj)
3
4function makeCalculation({x, y: d, z = 4}) {
5 return Math.floor((x + d + z) / 3)
6}
7
8// this is the same as
9function makeCalculation(obj) {
10 const {x, y: d, z = 4} = obj
11 return Math.floor((x + d + z) / 3)
12}
13
14// which is the same as
15function 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// in React:
23function 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:

1function nestedArrayAndObject() {
2 // refactor this to a single line of destructuring...
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 // const {} = info // <-- replace the next few `const` lines with this
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// add(1)
2// add(1, 2)
3function add(a, b = 0) {
4 return a + b
5}
6
7// is the same as
8const add = (a, b = 0) => a + b
9
10// is the same as
11function add(a, b) {
12 b = b === undefined ? 0 : b
13 return a + b
14}
15
16// in React:
17function 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

Rest/Spread

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.

1const arr = [5, 6, 8, 4, 9]
2Math.max(...arr)
3// is the same as
4Math.max.apply(null, arr)
5
6const 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}
15const 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}
23console.log({...obj1, ...obj2})
24// is the same as
25console.log(Object.assign({}, obj1, obj2))
26
27function add(first, ...rest) {
28 return rest.reduce((sum, next) => sum + next, first)
29}
30// is the same as
31function 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// in React:
38function 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.

1export default function add(a, b) {
2 return a + b
3}
4
5/*
6 * import add from './add'
7 * console.assert(add(3, 2) === 5)
8 */
9
10export const foo = 'bar'
11
12/*
13 * import {foo} from './foo'
14 * console.assert(foo === 'bar')
15 */
16
17export function subtract(a, b) {
18 return a - b
19}
20
21export const now = new Date()
22
23/*
24 * import {subtract, now} from './stuff'
25 * console.assert(subtract(4, 2) === 2)
26 * console.assert(now instanceof Date)
27 */
28
29// in React:
30import React, {Suspense, Fragment} from 'react'

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.

1const message = bottle.fullOfSoda
2 ? 'The bottle has soda!'
3 : 'The bottle may not have soda :-('
4
5// is the same as
6let message
7if (bottle.fullOfSoda) {
8 message = 'The bottle has soda!'
9} else {
10 message = 'The bottle may not have soda :-('
11}
12
13// in React:
14function 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:

1const 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
34dogs.find(dog => dog.name === 'Bernese Mountain Dog')
35// {id: 'dog-2', name: 'Bernese Mountain Dog', ...etc}
36
37dogs.some(dog => dog.temperament.includes('Aggressive'))
38// false
39
40dogs.some(dog => dog.temperament.includes('Trusting'))
41// true
42
43dogs.every(dog => dog.temperament.includes('Trusting'))
44// false
45
46dogs.every(dog => dog.temperament.includes('Intelligent'))
47// true
48
49dogs.map(dog => dog.name)
50// ['Poodle', 'Bernese Mountain Dog', 'Labrador Retriever']
51
52dogs.filter(dog => dog.temperament.includes('Faithful'))
53// [{id: 'dog-1', ..etc}, {id: 'dog-2', ...etc}]
54
55dogs.reduce((allTemperaments, dog) => {
56 return [...allTemperaments, ...dog.temperaments]
57}, [])
58// [ 'Intelligent', 'Active', 'Alert', ...etc ]
59
60// in React:
61function 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

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.

1function 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
17function 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}` // this would not be executed
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
43function log(...args) {
44 console.log(...args)
45}
46
47function logError(...args) {
48 console.error(...args)
49}
50
51// This is the mothership of all things asynchronous
52function 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// in React:
65function 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!

Discuss on TwitterEdit post on GitHub

Share article
loading relevant upcoming workshops...
Kent C. Dodds

Kent C. Dodds is a JavaScript software engineer and teacher. He's taught hundreds of thousands of people how to make the world a better place with quality software development tools and practices. He lives with his wife and four kids in Utah.