Sknoe / Notes

JavaScript Destructuring and Spread: React and API Examples

Learn to unpack object values with destructuring, then copy and update data with spread in React props and API responses.

4 min read

An API response can contain far more information than a component needs. A React component might receive a whole props object when it only needs a name and a role. Repeating long property paths makes that code harder to scan.

JavaScript gives us two related tools for working with objects: destructuring reads selected values into variables, and spread syntax copies properties into a new object. They both use braces and property names, but they do different jobs. This guide follows data from an API-shaped object into a component, then shows how to create an updated copy without changing the original.

What does object destructuring do?

javascript
const apiUser = {
id: 42,
name: 'Maya',
role: 'developer',
};
const { id, name, role } = apiUser;
console.log(name); // 'Maya'

The braces on the left form a destructuring pattern. JavaScript looks for properties named id, name, and role on apiUser, then assigns their values to variables with those names. The apiUser object is still there; destructuring does not remove or change its properties.

Rename a value or provide a default

javascript
const apiUser = { name: 'Maya' };
const { name: displayName, role = 'reader' } = apiUser;
console.log(displayName); // 'Maya'
console.log(role); // 'reader'

The pattern name: displayName reads the name property and stores its value in a variable called displayName. The default role value is used because apiUser.role is missing. A destructuring default applies when a property is missing or undefined; it does not replace null, false, or 0.

How does destructuring work with React props?

A function component receives its props as one object. Destructuring in the parameter list lets the component name the props it uses right away:

jsx
function ProfileCard({ user, compact = false }) {
const { name, role = 'Member' } = user;
return (
<article className={compact ? 'profile-card profile-card--compact' : 'profile-card'}>
<h2>{name}</h2>
<p>{role}</p>
</article>
);
}

The outer pattern reads the user and compact props. The inner pattern reads name and role from the user object. Writing function ProfileCard(props) and then using props.user would work too; parameter destructuring just brings the values you need into local variables sooner. React props are inputs, so treat them as read-only rather than changing the props object.

How can you unpack API data?

javascript
const apiResponse = {
user: {
id: 42,
name: 'Maya',
role: 'developer',
},
requestId: 'req-123',
};
const { user: { id, name, role } } = apiResponse;
console.log(name, role); // 'Maya' 'developer'

This pattern follows the shape of the data: apiResponse contains a user object, which contains id, name, and role. The nested pattern binds those three variables directly. It does not create a variable named user. If you also need the whole nested object, destructure it separately, for example const { user } = apiResponse.

What does object spread do?

javascript
const apiUser = { id: 42, name: 'Maya', role: 'developer' };
const updatedUser = { ...apiUser, role: 'designer' };
console.log(updatedUser);
// { id: 42, name: 'Maya', role: 'designer' }
console.log(apiUser.role); // 'developer'

The new object starts with the properties from apiUser. The role written after ...apiUser replaces the copied role, so updatedUser.role is designer while apiUser.role stays developer. This is a common way to build a changed object without assigning into the original.

Why does spread order matter?

javascript
const defaults = { theme: 'light', density: 'comfortable' };
const savedSettings = { theme: 'dark' };
const settings = { ...defaults, ...savedSettings };
console.log(settings.theme); // 'dark'

When a key appears more than once, the last value assigned wins. Here savedSettings comes after defaults, so its theme replaces the default. Reversing the order would make light win instead. When combining API data, defaults, and user choices, put them in the order that expresses which source should take priority.

How do you update nested API data safely?

javascript
const profile = {
name: 'Maya',
preferences: { theme: 'light', emailUpdates: true },
};
const updatedProfile = {
...profile,
preferences: {
...profile.preferences,
theme: 'dark',
},
};

The first spread makes a new top-level profile object. The second makes a new preferences object and changes only theme. This matters in React state updates and other code that relies on leaving previous data unchanged. Copying only the top-level object would still leave the nested preferences object shared.

Under the hoodObject spread makes a shallow copyA copied outer object can still point to the same nested objects as the original.

Looking closer

Object spread copies own enumerable properties into a new object, one level at a time. If a property value is itself an object or array, the new object receives that same nested reference. So const copy = { ...profile } creates a new outer object, but copy.preferences === profile.preferences is still true. To change a nested value without sharing that nested object, copy each level you change, as in the updatedProfile example. Spread is not a general deep-cloning tool.

Destructuring and spread: what is the difference?

javascript
const { name } = apiUser; // read a property into a variable
const renamed = { ...apiUser, name: 'Maya R.' }; // build a new object

The punctuation looks similar, but the direction is different. Destructuring takes named values out of an object for your code to use. Spread takes properties from an object and puts them into another object. In React, you may also see JSX spread such as <ProfileCard {...props} />; that passes the props object's entries as component props. Use it when forwarding a known set of props, and avoid forwarding arbitrary data without checking what the child should receive.

What to learn next

  • Objects are data; functions do work

    Destructuring reads object data while functions decide what to do with it

    A short practical companion about recognizing configuration objects and the functions that process them.

  • React props

    Props are an object passed into a component

    Continue with how parent components pass values and how child components read them.

  • Array methods

    Destructuring and spread also work with arrays

    Apply the same ideas to array values and then see how map and filter transform collections.

When you read existing code, ask two questions: is this pattern taking values out of an object, or putting properties into a new one? Then check whether the copied values include nested objects and which spread source appears last. Those details explain most destructuring and object-spread behavior you will meet in component props and API data.

Did this click?

Tap the bulb if this explanation helped.

Further reading

Interview questions

What is object destructuring in JavaScript?

Object destructuring reads selected properties from an object into variables. For example, const { name } = user creates a variable named name from user.name; it does not change the user object.

What is the difference between object destructuring and object spread?

Destructuring reads selected property values out of an object into variables. Object spread copies an object's own enumerable properties into a new object. Destructuring is for using values; spread is for building an object.

Does object spread create a deep copy?

No. Object spread makes a shallow copy. It creates a new outer object, but nested objects and arrays are still shared references unless you copy those levels too.

When two spread objects have the same property, which value is used?

The value assigned last wins. In { ...defaults, ...savedSettings }, a property from savedSettings replaces the same property from defaults.

Do destructuring defaults replace null values?

No. A default is used when the property is missing or its value is undefined. If the property is null, the variable receives null.