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.

Answer from goto on Stack Overflow
🌐
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.
🌐
MDN Web Docs
developer.mozilla.org › en-US › docs › Web › JavaScript › Reference › Operators › Spread_syntax
Spread syntax (...) - JavaScript | MDN
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 ...
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
reactjs - Spread operator in react component props - Stack Overflow
Could someone tell me, how it is 3 dot spread does? I know it wanna passing a isActive(Boolean) into Component SideNavLink. If it true then it has those props. But, I'm wondering what the code does... More on stackoverflow.com
🌐 stackoverflow.com
The spread operator for props {...props} allows you to pass all props at once.
The downside is you are passing all the props to components that might not require all of the values. More on reddit.com
🌐 r/threejs
22
0
May 3, 2024
Curly braces and the spread operator questions
Yes, JSX specifically has its own additional spread operator: https://legacy.reactjs.org/docs/jsx-in-depth.html#spread-attributes More on reddit.com
🌐 r/reactjs
3
2
March 28, 2024
🌐
DEV Community
dev.to › marinamosti › understanding-the-spread-operator-in-javascript-485j
Understanding the Spread Operator in JavaScript - DEV Community
September 23, 2019 - In frameworks like Vue this becomes ... never want to modify the original object. In React this is also used when changing the state, you're not supposed to directly modify the value. So how do we make a copy with the spread operator?...
🌐
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?).
🌐
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 - Introduction In React.js, the three dots (...), commonly known as the spread operator, are a powerful tool for managing state, props, and arrays within your applications. The spread syntax simplifies many common tasks, such as merging objects, ...
Find elsewhere
🌐
Telerik
telerik.com › blogs › rest-spread-operators-explained-javascript
Rest and Spread Operators Explained in JavaScript
February 27, 2024 - This code sample summarizes our definition of rest: The rest operator combines elements or arguments of a function into an array. The spread operator divides an array or object into separate elements or 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"]
});
🌐
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!
🌐
JavaScript.info
javascript.info › tutorial › the javascript language › advanced working with functions
Rest parameters and spread syntax
October 18, 2022 - Array.from operates on both array-likes and iterables. The spread syntax works only with iterables.
🌐
Js
reactpatterns.js.org › docs › jsx-spread-attributes
JSX Spread Attributes | reactpatterns
⭐️ If you like reactpatterns, give it a star on GitHub! ⭐️ ... Spread attributes is a JSX feature, it's a syntax for passing all of an object's properties as JSX attributes.
🌐
Stackademic
blog.stackademic.com › spread-operator-in-javascript-and-reactjs-a-quick-guide-8db6a19d5e37
Spread Operator in JavaScript and React: A Quick Guide | by Oluwaseun | Stackademic
October 17, 2023 - In the example, the spread operator is used to pass the user object’s properties (name, age, and gender) as individual props to the ChildComponent at once rather than passing the props one after the other. When updating a state in Reactjs, it is important to create a copy of the object or array we intend to update in order to preserve the original state or to make it immutable (unchanged).
🌐
Kinsta®
kinsta.com › home › resource center › blog › javascript tutorials › unleashing the power of javascript spread operator
Unleashing the Power of JavaScript Spread Operator - Kinsta®
October 1, 2025 - Learn how to unleash the power of the spread operator in JavaScript. The easy-to-follow guide shows you just how todo that.
🌐
Js
reactpatterns.js.org › docs › destructuring-rest-or-spread-operator
Destructuring Rest/Spread Operator | reactpatterns
⭐️ If you like reactpatterns, give it a star on GitHub! ⭐️ ... The ... rest operator gathers the rest of the items in the props object argument and puts them in the variable rest. The ... rest in the JSX is actually JSX syntax for spreading the props in the the rest object into individual props.
🌐
LinkedIn
linkedin.com › all › front-end development
How can you simplify your code with spread and rest operators?
August 17, 2023 - The spread operator (...) allows you to expand an iterable object, such as an array or a string, into individual elements. For example, you can use it to copy an array, concatenate multiple arrays, or pass an array as arguments to a function.
🌐
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.
🌐
DhiWise
dhiwise.com › blog › design-converter › how-to-use-spread-props-in-react-for-better-components
Spread Props React Guide: Write Efficient Components Easily
January 17, 2025 - The spread operator, denoted by ..., is a powerful feature in JavaScript that simplifies passing props to React components. When you have an object containing multiple properties, you can easily spread these properties into 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 - Spread Operator The spread operator, which was introduced in ES6, is a magical operator that allows you to pass arrays, objects, and props in such a way that they make your react components more dynamic and flexible.