avatarDr. Derek Austin 🥳

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

2549

Abstract

use it helps me write cleaner code:</p><div id="b55b" class="link-block"> <a href="https://javascript.plainenglish.io/destructure-react-props-with-es6-object-destructuring-for-cleaner-code-3984453e484d"> <div> <div> <h2>Destructure React Props with ES6 Object Destructuring for Cleaner Code</h2> <div><h3>When you write a render function for a component in React, it takes in a props object containing the properties…</h3></div> <div><p>javascript.plainenglish.io</p></div> </div> <div> <div style="background-image: url(https://miro.readmedium.com/v2/resize:fit:320/0*9CZu2_V6BYqEAyy_)"></div> </div> </div> </a> </div><p id="39aa">But today I want to tackle partial object destructuring, which is when you only need to access a part of the <code>props</code> object.</p><p id="b17f">A common use case would be when your component may be receiving dozens of <code>props</code>, but you only want to access one or two of them. Instead of writing out a bunch of <code>props</code> that don’t even get used by the component, you can instead only destructure the <code>props</code> that you’re going to need.</p><figure id="5a79"><img src="https://cdn-images-1.readmedium.com/v2/resize:fit:800/0*-xXtonTkahBIGa98"><figcaption>Photo by <a href="https://unsplash.com/@cslangston?utm_source=medium&amp;utm_medium=referral">@Chrissy Langston</a> on <a href="https://unsplash.com?utm_source=medium&amp;utm_medium=referral">Unsplash</a></figcaption></figure><h1 id="8c28">How To Partially Destructure the React props Object</h1><p id="0cb1">If you’re working with a large <code>props</code> object that you don’t want to destructure completely, you just destructure the <code>props</code> later in the code:</p><figure id="0c7b"><img src="https://cdn-images-1.readmedium.com/v2/resize:fit:800/1*ITj8TXgQkerGXyfGVc7VNg.png"><figcaption><a href="https://gist.github.com/DoctorDerek/aba7492bc6c7fcb592b0d5f5e3e60a43">View raw code</a> as a GitHub Gist</figcaption></figure><p id="0b80">Compared to destructuring all of the <code>props</code>, where I always do it right in the parameters, it’s convenient to partially destructure later in the code.</p><p id="2af6">Then, you can use<a href="https://readmedium.com/how-to-use-the-spread-operator-in-javascript-b9e4a8b06fab"> the <code></code>... spread operator</a> again to pass on all <code>props</code> to the child component with the sy

Options

ntax<code>{...props}</code>, if that’s what you need.</p><p id="7daa">As an alternative , you can partially destructure props using <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions/rest_parameters">the <code>.</code>.. rest parameter</a> in the render function’s arguments, same as later in the code:</p><figure id="703f"><img src="https://cdn-images-1.readmedium.com/v2/resize:fit:800/1*HytuyiN_iulPU2_K4-fPTg.png"><figcaption><a href="https://gist.github.com/DoctorDerek/31f212e4e14a102a3978a0beb9b73f5b">View raw code</a> as a GitHub Gist</figcaption></figure><p id="5d77">Here, we partially destructure in the parameters because we expect certain <code>props</code>, then we get the other <code>props</code> with the <code>...</code> rest parameter.</p><p id="c041">And that’s all there is to partially destructuring props! Personally, I avoid this pattern, and I always completely destructure function parameters:</p><div id="3fd2" class="link-block"> <a href="https://readmedium.com/why-you-should-always-pass-objects-as-function-parameters-in-javascript-7fb7c5833dc6"> <div> <div> <h2>Why You Should Always Pass Objects as Function Parameters in JavaScript</h2> <div><h3>This code pattern, which is commonly used in React, is a much better idea than using traditional function parameters in…</h3></div> <div><p>medium.com</p></div> </div> <div> <div style="background-image: url(https://miro.readmedium.com/v2/resize:fit:320/0*Ui1k38gEy2sbUXrB)"></div> </div> </div> </a> </div><p id="9bbb">But you may prefer to partially destructure the <code>props</code> object, especially if you’re passing around 20 or 30 props at a time, since it results in less code.</p><p id="3ca8"><b>Happy coding! 🐶</b></p><figure id="642a"><img src="https://cdn-images-1.readmedium.com/v2/resize:fit:800/0*7GLuCAZJbpaF10gh"><figcaption>Photo by <a href="https://unsplash.com/es/@lindaseger?utm_source=medium&amp;utm_medium=referral">Linda Segerfeldt</a> on <a href="https://unsplash.com?utm_source=medium&amp;utm_medium=referral">Unsplash</a></figcaption></figure><p id="4edb"><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>

Partial Object Destructuring of React Props With ... Rest Parameters Syntax

When working with the React props object, it can be very helpful to use ES6 object destructuring to make your code clearer. However, sometimes you only want to destructure some of the props using partial object destructuring. Here’s how to do it.

Photo by Josh Rakower on Unsplash

Why Partially Destructure React Props?

The React props object is the name for the properties that you pass in to a component in React. You always pass the props (short for properties) as the sole argument to the render function in the React component.

You access props using props.name in a function component or this.props.name in a class component. A common reason you’ll need props is if you’re “lifting state up” and then passing it to the component:

Since props is just an object, you can destructure its properties like any other JavaScript object using the ES6 object destructuring syntax. You can easily turn props.name into just name, resulting in easier-to-read code.

I’ve talked before about destructuring all React props at the same time, which is a strategy I love because it helps me write cleaner code:

But today I want to tackle partial object destructuring, which is when you only need to access a part of the props object.

A common use case would be when your component may be receiving dozens of props, but you only want to access one or two of them. Instead of writing out a bunch of props that don’t even get used by the component, you can instead only destructure the props that you’re going to need.

Photo by @Chrissy Langston on Unsplash

How To Partially Destructure the React props Object

If you’re working with a large props object that you don’t want to destructure completely, you just destructure the props later in the code:

View raw code as a GitHub Gist

Compared to destructuring all of the props, where I always do it right in the parameters, it’s convenient to partially destructure later in the code.

Then, you can use the ... spread operator again to pass on all props to the child component with the syntax{...props}, if that’s what you need.

As an alternative , you can partially destructure props using the ... rest parameter in the render function’s arguments, same as later in the code:

View raw code as a GitHub Gist

Here, we partially destructure in the parameters because we expect certain props, then we get the other props with the ... rest parameter.

And that’s all there is to partially destructuring props! Personally, I avoid this pattern, and I always completely destructure function parameters:

But you may prefer to partially destructure the props object, especially if you’re passing around 20 or 30 props at a time, since it results in less code.

Happy coding! 🐶

Photo by Linda Segerfeldt on Unsplash

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
Software Engineering
JavaScript
React
Web Development
Recommended from ReadMedium