avatarDr. Derek Austin 🥳

Free AI web copilot to create summaries, insights and extended knowledge, download it at here

3990

Abstract

torDerek/c4137b083fabc39e0280baae3a49e797">View raw code</a> as a GitHub Gist</figcaption></figure><p id="0921">While on first glance destructuring seems like a bit of a pointless trick (<i>Who needs more variables?</i>), I love to use props destructuring in my React code.</p><h1 id="027c">What’s the Advantage of Destructuring?</h1><p id="2937">When working with props in React, your code can end up a mess of <code>this.props.someProp</code> and <code>this.props.otherProp</code> — making it hard to read and potentially more prone to simple typos as you’re developing.</p><blockquote id="5b81"><p>“With destructuring assignment, the equivalent code becomes more concise and readable.” — <a href="undefined">Nick Fitzgerald</a> and <a href="undefined">Jason Orendorff</a> <a href="https://hacks.mozilla.org/2015/05/es6-in-depth-destructuring/">in the Mozilla Hacks blog</a></p></blockquote><p id="7bce">When instead of <code>props.name</code> (in a <a href="https://reactjs.org/docs/components-and-props.html#function-and-class-components">function component</a>) or <code>this.props.name</code> (in a <a href="https://reactjs.org/docs/components-and-props.html#function-and-class-components">class component</a>) we can just say <code>name</code>, then our code becomes a lot clearer to us and any other developers reading the code.</p><blockquote id="391b"><p>“Destructuring really shines in React apps, where it can greatly simplify how you write props.” — <a href="undefined">Lindsay Criswell</a> <a href="https://readmedium.com/destructuring-props-in-react-b1c295005ce0">on her Medium blog</a></p></blockquote><p id="f48b">It might not mean a lot, but you could easily type out <code>this.props.</code> hundreds of times in a React codebase. Instead, by using ES6 object destructuring on the <code>props</code> object, you can make your code much more straightforward.</p><h1 id="b957">Props Destructuring in a Function Component</h1><p id="3b6c">Let’s take a look at a code example where we’re passing in a name and an emoji to a component that will display it.</p><figure id="9b2c"><img src="https://cdn-images-1.readmedium.com/v2/resize:fit:800/1*Aex93Su7rVayR1kSpR61yw.png"><figcaption><a href="https://gist.github.com/DoctorDerek/07f94cc733865dfe500c76270af4999c">View raw code</a> as a GitHub Gist</figcaption></figure><p id="fbee">Particularly when you’re using a prop repeatedly, you start to save a lot of typing when you can refer to <code>{emoji}</code> instead of <code>{props.emoji}</code>.</p><p id="59d0">Here’s the same exact React function component without destructuring:</p><figure id="f159"><img src="https://cdn-images-1.readmedium.com/v2/resize:fit:800/1*yj6s59mY2wLmmFZNd1rD4g.png"><figcaption><a href="https://gist.github.com/DoctorDerek/45bbd012ef3198f54affb2fb24006407">View raw code</a> as a GitHub Gist</figcaption></figure><p id="d121">It’s not a big difference in such a small component, but in a React component with dozens of props, destructuring can be a big deal for readability.</p><p id="a250">Specifying the props explicitly with destructuring is also a reminder to yourself and other developers which props you are expecting — something that is very easy to lose track of when you never destructure the props.</p><h1 id="f9c6">Props Destructuring in a Class Component</h1><p id="6291">I prefer using function components in React, but ES6 destructuring will work just the same in React class components.</p><figure id="cc52"><img src="https://cdn-images-1.readmedium.com/v2/resize:fit:800/1*QnABQBZjmcVlEl5TEJsNQg.png"><figcaption><a href="https://gist.github.com/DoctorDerek/b20bff911c44717fa0c9b2ed3c0fff87">View raw code</a> as a GitHub Gist</figcaption></figure><p id="7d53">Destructuring the props makes your code a lot more readable than sifting through dozens of <code>this.props.someProp</code> references in your code.</p><h1 id="500a">Props Destructuring in a Function’s Arguments</h1><p id="5b99">For even better readability, we can d

Options

estructure the <code>props</code> object right in the render function’s arguments. Destructuring works the same way in a function declaration as it does with variable assignments.</p><p id="35af">That means you can destructure the props objects right in the arguments: <code>render(props)</code> becomes <code>render({name, emoji})</code>. If that doesn’t make a lot of sense to you, then <a href="undefined">Lindsay Criswell</a> <a href="https://readmedium.com/destructuring-props-in-react-b1c295005ce0">has a great post</a> on her Medium blog explaining how to destructure props in the render function’s arguments.</p><p id="ff45">Here’s the example of the same function component from before, but I moved the destructuring up into the arguments for the function.</p><figure id="9267"><img src="https://cdn-images-1.readmedium.com/v2/resize:fit:800/1*F6PHiem6SuTdAWDB_X2K8Q.png"><figcaption><a href="https://gist.github.com/DoctorDerek/acd59884d2a41726208df5ea19f0212f">View raw code</a> as a GitHub Gist</figcaption></figure><p id="6e01">In this example, I extract the <code>name</code> and <code>emoji</code> properties from <code>props</code> by using the destructuring syntax in the <code>render</code> function’s arguments.</p><p id="3f5b">A lot of developers prefer this syntax for destructuring React props — you never actually need to type the word <code>props</code>. 😂</p><h1 id="1f36">Conclusion: Destructured React Props Are Better</h1><p id="fa0d">Should you always destructure your props in React? Well, maybe not every time, but you should at least be familiar with the syntax to do so.</p><p id="5ff1">I think React components become a lot more readable with destructuring the props object, and I’m always an advocate for <a href="https://readmedium.com/why-you-should-make-your-code-as-simple-as-possible-3b35e89f137">simple code</a>.</p><p id="549f">Destructuring React props works great particularly when you’re passing around all of the props at the same time, as I covered <a href="https://readmedium.com/how-to-pass-all-props-to-a-child-component-in-react-bded9e38bb62">in Coding at Dawn</a>:</p><div id="2158" class="link-block"> <a href="https://readmedium.com/how-to-pass-all-props-to-a-child-component-in-react-bded9e38bb62"> <div> <div> <h2>How to Pass All Props to a Child Component in React</h2> <div><h3>Being able to share all props from parent to child component in React lets you easily break your app up into small…</h3></div> <div><p>medium.com</p></div> </div> <div> <div style="background-image: url(https://miro.readmedium.com/v2/resize:fit:320/0*3WBlwP9u4_ixAqHF)"></div> </div> </div> </a> </div><p id="4340">If you need the <code>props</code> object to pass on all of the props to a child component using the <code>...</code> spread operator (<code>{...props}</code>), then you wouldn’t want to destructure <code>props</code> in the arguments to the function <code>render(props)</code>.</p><p id="804f">Instead, you would want to destructure <code>props</code> at the top of the render function using the assignment syntax (with <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Assignment">the <cod< code="">e>= operator</cod<></a>), leaving the <code>props</code> object intact for you to be able to pass it on to a child component.</p><p id="0fe6">Hopefully this has been helpful to you in understanding the usefulness of the ES6 object destructuring syntax when working with React props.</p><p id="742c"><b>Happy coding! 💻 (☞゚ヮ゚)☞ 🤑</b></p><p id="40de"><a href="https://www.linkedin.com/in/derek-austin/">Dr. Derek Austin</a> is the author of <a href="https://www.amazon.com/dp/B0BRJDLJ43"><i>Career Programming: How You Can Become a Successful 6-Figure Programmer in 6 Months</i></a>, now available on Amazon.</p></article></body>

Destructure React Props with ES6 Object Destructuring for Cleaner Code

When you write a render function for a component in React, it takes in a props object containing the properties (“props”) that you’ll use to configure that component. Props make components reusable. To write cleaner code, you can destructure the props object into separate variables using ES6 object destructuring.

Photo by ibrahim sabuncuoğlu on Unsplash

Using ES6 Object Destructuring in React

The React props object is made up of the properties (“props” for short) passed into a component by its parent component in React. Having props is what makes React components reusable and modular.

The props object is the sole argument to React’s render function (render(props)). You pass props from the parent component in the JSX:

View raw code as a GitHub Gist

Typically, we extract properties from props using the e>. syntax, as the props object is just a typical JavaScript object. For example, we might call props.name in a React function component or this.props.name in a React class component. There’s a better syntax, though: ES6 object destructuring.

“The destructuring assignment syntax is a JavaScript expression that makes it possible to unpack values from arrays, or properties from objects, into distinct variables.” — MDN Docs

If you’ve tried using the useState hook (one of the React Hooks), then you’ve already seen the array destructuring syntax:

View raw code as a GitHub Gist

Object destructuring works similarly for objects, allowing you to extract their properties into variables with the same name:

View raw code as a GitHub Gist

While on first glance destructuring seems like a bit of a pointless trick (Who needs more variables?), I love to use props destructuring in my React code.

What’s the Advantage of Destructuring?

When working with props in React, your code can end up a mess of this.props.someProp and this.props.otherProp — making it hard to read and potentially more prone to simple typos as you’re developing.

“With destructuring assignment, the equivalent code becomes more concise and readable.” — Nick Fitzgerald and Jason Orendorff in the Mozilla Hacks blog

When instead of props.name (in a function component) or this.props.name (in a class component) we can just say name, then our code becomes a lot clearer to us and any other developers reading the code.

“Destructuring really shines in React apps, where it can greatly simplify how you write props.” — Lindsay Criswell on her Medium blog

It might not mean a lot, but you could easily type out this.props. hundreds of times in a React codebase. Instead, by using ES6 object destructuring on the props object, you can make your code much more straightforward.

Props Destructuring in a Function Component

Let’s take a look at a code example where we’re passing in a name and an emoji to a component that will display it.

View raw code as a GitHub Gist

Particularly when you’re using a prop repeatedly, you start to save a lot of typing when you can refer to {emoji} instead of {props.emoji}.

Here’s the same exact React function component without destructuring:

View raw code as a GitHub Gist

It’s not a big difference in such a small component, but in a React component with dozens of props, destructuring can be a big deal for readability.

Specifying the props explicitly with destructuring is also a reminder to yourself and other developers which props you are expecting — something that is very easy to lose track of when you never destructure the props.

Props Destructuring in a Class Component

I prefer using function components in React, but ES6 destructuring will work just the same in React class components.

View raw code as a GitHub Gist

Destructuring the props makes your code a lot more readable than sifting through dozens of this.props.someProp references in your code.

Props Destructuring in a Function’s Arguments

For even better readability, we can destructure the props object right in the render function’s arguments. Destructuring works the same way in a function declaration as it does with variable assignments.

That means you can destructure the props objects right in the arguments: render(props) becomes render({name, emoji}). If that doesn’t make a lot of sense to you, then Lindsay Criswell has a great post on her Medium blog explaining how to destructure props in the render function’s arguments.

Here’s the example of the same function component from before, but I moved the destructuring up into the arguments for the function.

View raw code as a GitHub Gist

In this example, I extract the name and emoji properties from props by using the destructuring syntax in the render function’s arguments.

A lot of developers prefer this syntax for destructuring React props — you never actually need to type the word props. 😂

Conclusion: Destructured React Props Are Better

Should you always destructure your props in React? Well, maybe not every time, but you should at least be familiar with the syntax to do so.

I think React components become a lot more readable with destructuring the props object, and I’m always an advocate for simple code.

Destructuring React props works great particularly when you’re passing around all of the props at the same time, as I covered in Coding at Dawn:

If you need the props object to pass on all of the props to a child component using the ... spread operator ({...props}), then you wouldn’t want to destructure props in the arguments to the function render(props).

Instead, you would want to destructure props at the top of the render function using the assignment syntax (with the e>= operator), leaving the props object intact for you to be able to pass it on to a child component.

Hopefully this has been helpful to you in understanding the usefulness of the ES6 object destructuring syntax when working with React props.

Happy coding! 💻 (☞゚ヮ゚)☞ 🤑

Dr. Derek Austin is the author of Career Programming: How You Can Become a Successful 6-Figure Programmer in 6 Months, now available on Amazon.

Programming
React
JavaScript
Software Engineering
Web Development
Recommended from ReadMedium