🌐
W3Schools
w3schools.com β€Ί react β€Ί react_es6_spread.asp
React ES6 Spread Operator
React Compiler React Quiz React Exercises React Syllabus React Study Plan React Server React Interview Prep React Bootcamp ... The JavaScript spread operator (...) copies all or part of an existing array or object into another array or object.
Discussions

Why use the spread operator when calling 'setState()' in React?
I just start picking up react.js so I went through a lot of tutorials and I've stumbled upon this bit which basically meant to delete an item from the state. this is how the guy introduced to me the More on stackoverflow.com
🌐 stackoverflow.com
Spread operator in React .setState() - javascript
0 Unhandled Rejection (TypeError): Cannot read property 'protocol' of undefined - React Hooks with axios Β· 0 How to prevent duplicate images when uploaded to firebase? 0 Using spread operator to setState in multiple succession More on stackoverflow.com
🌐 stackoverflow.com
How do you even get a kill in this game? : r/VALORANT
🌐 r/VALORANT
What is the point of using the spread operator to pass props while also passing props individually?
...props contains all the properties that are not destructured from the object. It's called the rest operator. https://www.freecodecamp.org/news/javascript-rest-vs-spread-operators/ More on reddit.com
🌐 r/react
2
3
August 31, 2022
🌐
CoreUI
coreui.io β€Ί blog β€Ί draft-how-to-replace-all-occurrences-of-a-string-in-javascript
Mastering the Spread Operator (<code>...</code>) in React.js Β· CoreUI
September 3, 2023 - This method is particularly effective ... React state, the spread operator simplifies the process of creating a new array that includes updates or additions without mutating the existing array....
🌐
Scrimba
scrimba.com β€Ί articles β€Ί react-spread-operator
How to use the spread operator (...) in React
January 31, 2023 - In the context of React, the spread operator can be used to spread an object of props onto an element in a React component, making it easier to pass down props to child components but more on that later!
🌐
DEV Community
dev.to β€Ί edriso β€Ί using-the-spread-operator-in-react-426e
Using the Spread Operator in React - DEV Community
January 5, 2026 - In JavaScript, the spread operator (...) allows you to spread the properties of an object or elements of an array into individual items. In React, it's commonly used to pass all properties of an object as props to a component.
🌐
Medium
medium.com β€Ί @thejasonfile β€Ί using-the-spread-operator-in-react-setstate-c8a14fc51be1
Using the spread operator in React setState | by Jason Arnold | Medium
May 25, 2017 - That’s when I remembered the spread operator […]. The spread operator can be used to take an existing array and add another element to it while still preserving the original array (famous original array’s?).
🌐
Medium
medium.com β€Ί @fa20-bse-059 β€Ί understanding-the-spread-operator-in-react-native-21be8f660d65
Understanding the Spread Operator in React Native | by saad | Medium
January 7, 2025 - Let’s say you have a list of ... When working with React Native components, the spread operator can be used to pass props to child components dynamically....
🌐
CoderCrafter
codercrafter.in β€Ί home β€Ί blog β€Ί master the react es6 spread operator: a complete guide with examples & best practices
Master the React ES6 Spread Operator: A Complete Guide with Examples & Best Practices | CoderCrafter
October 10, 2025 - This is a fancy word for a simple concept: don't change the original data. Instead, create a new, updated copy of the data. This allows React to efficiently detect changes and know when to re-render your components. The spread operator is the perfect tool for this job.
Find elsewhere
🌐
Medium
medium.com β€Ί @pojotorshemi β€Ί uses-of-spread-operator-in-javascript-react-f5f456186e63
Uses of Spread Operator in JavaScript, React. | by akpojotor shemi | Medium
July 27, 2020 - Spread operator allows an iterable ... (for object literals) are expected. Spread operator could be use to simplify passing react props down to a component....
🌐
Medium
medium.com β€Ί @finnkumar6 β€Ί chapter-11-mastering-the-es6-spread-operator-in-react-with-examples-real-world-use-cases-3d0fb864c4e7
Chapter 11:πŸš€ Mastering the ES6 Spread Operator in React (With Examples & Real-World Use Cases) | by Aryan kumar | Medium
February 1, 2025 - After finishing this, you will be using … in your React projects as a pro. Now, grab a coffee β˜• and sit down, let us dive into it. So simply the spread operator (…) lets you take an array or object and spread them into a new array or object.
🌐
DhiWise
dhiwise.com β€Ί post β€Ί how-to-simplify-your-react-code-with-the-spread-operator
The Ultimate Guide To Using The React Spread Operator
October 31, 2023 - It creates a new array or object instead of mutating the existing one, which is a key principle in React. Versatility: The spread operator can be used in various scenarios, such as copying objects, merging arrays, passing props, and more.
🌐
Medium
medium.com β€Ί @fa22-bse-004 β€Ί how-to-use-the-spread-operator-in-react-eb6e071b57f4
How to use the spread operator (…) in React | by maryam hafeez | Medium
September 22, 2024 - A spread operator spreads a multiple array of objects providing it with the structure of a single array which can consists of further arrays or objects or any property, Like copying multiple arrays into one array as a separate array.
🌐
MDN Web Docs
developer.mozilla.org β€Ί en-US β€Ί docs β€Ί Web β€Ί JavaScript β€Ί Reference β€Ί Operators β€Ί Spread_syntax
Spread syntax (...) - JavaScript - MDN Web Docs - Mozilla
May 22, 2026 - The spread (...) syntax allows an iterable, such as an array or string, to be expanded in places where zero or more arguments (for function calls) or elements (for array literals) are expected. In an object literal, the spread syntax enumerates the properties of an object and adds the key-value ...
🌐
Medium
medium.com β€Ί @malkamalik007 β€Ί spread-operator-in-react-905a25a15a7a
Spread Operator (...) in React
September 22, 2024 - The spread operator (…) is considered ... iterables. In React, the spread operator is used to ease the operations of passing props, handling state and manipulating component properties....
Top answer
1 of 4
4

As per the documentation:

Never mutate this.state directly, as calling setState() afterwards may replace the mutation you made. Treat this.state as if it were immutable.

  • reactjs.org/docs/react-component.html#state

So, in the example from the tutorial you've mentioned, you wouldn't need to make a copy of the array to update your state.

// GOOD
delTodo = id => {
  this.setState({
    todos: this.state.todos.filter(...)
  })
}

Array.filter method creates a new array and does not mutate the original array, therefore it won't directly mutate your state. Same thing applies to methods such as Array.map or Array.concat.

If your state is an array and you're applying methods that are mutable, you should copy your array.

See more to figure out which Array methods are mutable:

  • doesitmutate.xyz

However, if you were to do something like the following:

// BAD
delTodo = id => {
  const todos = this.state.todos
  todos.splice(id, 1)
  this.setState({ todos: todos })
}

Then you'd be mutating your state directly, because Array.splice changes the content of an existing array, rather than returning a new array after deleting the specific item. Therefore, you should copy your array with the spread operator.

// GOOD
delTodo = id => {
  const todos = [...this.state.todos]
  todos.splice(id, 1)
  this.setState({ todos: todos })
}

Similarly with objects, you should apply the same technique.

// BAD
updateFoo = () => {
  const foo = this.state.foo // `foo` is an object {}
  foo.bar = "HelloWorld"
  this.setState({ foo: foo })
}

The above directly mutates your state, so you should make a copy and then update your state.

// GOOD
updateFoo = () => {
  const foo = {...this.state.foo} // `foo` is an object {}
  foo.bar = "HelloWorld"
  this.setState({ foo: foo })
}

Hope this helps.

2 of 4
3

Why use the spread operator at all?

The spread operator ... is often used for creating shallow copies of arrays or objects. This is especially useful when you aim to avoid mutating values, which is encouraged for different reasons. TLDR; Code with immutable values is much easier to reason about. Long answer here.

Why is the spread operator used so commonly in react?

In react, it is strongly recommended to avoid mutation of this.state and instead call this.setState(newState). Mutating state directly will not trigger a re-render, and may lead to poor UX, unexpected behavior, or even bugs. This is because it may cause the internal state to differ from the state that is being rendered.

To avoid manipulating values, it has become common practice to use the spread operator to create derivatives of objects (or arrays), without mutating the original:

// current state
let initialState = {
    user: "Bastian",
    activeTodo: "do nothing",
    todos: ["do nothing"]
}


function addNewTodo(newTodo) {
    // - first spread state, to copy over the current state and avoid mutation
    // - then set the fields you wish to modify
    this.setState({
        ...this.state,
        activeTodo: newTodo,
        todos: [...this.state.todos, newTodo]
    })
}

// updating state like this...
addNewTodo("go for a run")
// results in the initial state to be replaced by this:
let updatedState = {
    user: "Bastian",
    activeTodo: "go for a run",
    todos: ["do nothing", "go for a run"]
}

Why is the spread operator used in the example?

Probably to avoid accidental state mutation. While Array.filter() does not mutate the original array and is safe to use on react state, there are several other methods which do mutate the original array, and should not be used on state. For example: .push(), .pop(),.splice(). By spreading the array before calling an operation on it, you ensure that you are not mutating state. That being said, I believe the author made a typo and instead was going for this:

 delTodo = id => {
    this.setState({
      todos: [...this.state.todos].filter(todo => todo.id !== id)
    });
  };

If you have a need to use one of the mutating functions, you can choose to use them with spread in the following manner, to avoid mutating state and potentially causing bugs in your application:

// here we mutate the copied array, before we set it as the new state
// note that we spread BEFORE using an array method
this.setState({
      todos: [...this.state.todos].push("new todo")
});

// in this case, you can also avoid mutation alltogether:
this.setState({
      todos: [...this.state.todos, "new todo"]
});
🌐
Medium
medium.com β€Ί @truongtronghai β€Ί spread-operator-ba867a0e97b4
Spread operator. Some ways to use it in React Js | by Truong Trong Hai | Medium
October 3, 2024 - Spread operator Some ways to use it in React Js 1. Spreading Props You can use the spread operator to pass all properties of an object as props to a component. const cardProps = { title: "Hello …
🌐
DEV Community
dev.to β€Ί tylerasa β€Ί spread-them-leveraging-the-spread-operator-for-safer-state-updates-in-react-30gm
Spread Them: Leveraging the Spread Operator for Safer State Updates in React - DEV Community
June 20, 2023 - Using the spread operator allows us to create copies of objects or arrays by value, ensuring that any changes made to the copy do not affect the original state. This approach maintains immutability and enables React's shallow comparison algorithm ...
🌐
LinkedIn
linkedin.com β€Ί pulse β€Ί react-spread-operator-rogers-muyinda
React spread operator {...}
October 6, 2023 - Spread Properties: In object destructuring, you can use the spread operator to extract specific properties from an object and assign them to a new object. For example: const person = { name: "Alice", age: 25, city: "New York" }; ... Here, name ...
🌐
OneCompiler
onecompiler.com β€Ί javascript β€Ί 3yyj44jxk
functions & spread operator in react js - JavaScript - OneCompiler
Below are couple of ways to use arrow function but it can be written in many other ways as well. ... const numbers = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] const squaresOfEvenNumbers = numbers .filter((ele) => ele % 2 == 0) .map((ele) => ele ** 2) console.log(squaresOfEvenNumbers) ... //Object spread const post = { ...options, type: "new", } //array spread const users = [...adminUsers, ...normalUsers]