Both works in that case, but you don't need to use that. Just setting the state will be okay:

this.setState({active: false})

But let me explain what if you have nested level of states like:

state = {
  foo: {
   a: 1,
   b: 2,
   c: 3
  }
}

And when you need to update the foo's c state only, then you'll need to merge the state like:

this.setState({ foo: {
  ...this.state.foo,
  c: 'updated value'
}})

So, the spread syntax merges object with later object. It's similar to Object.assign.

Answer from Bhojendra Rauniyar on Stack Overflow
🌐
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?). var colors = ['red', 'green', ...
Discussions

Changing a React State Object with Spread Operator
I'm trying to validate a form before submting it,and I created an object of possible errors, but when try to change the value of each key it behaves weirdly... const inialState = { name: "&q... More on stackoverflow.com
🌐 stackoverflow.com
Why use the spread operator when calling 'setState()' in React?
If you mutate state (which keeps ref same) React may not be able to ascertain which part of your state actually changed and probably construct a tree on next render which is different from expected. ... Save this answer. ... Show activity on this post. The spread operator that the guy's code ... More on stackoverflow.com
🌐 stackoverflow.com
reactjs - Spread operator in React .setState() in the useState Hook - Stack Overflow
Why is the spread operator used in setName() function how does it work. What this code does is it takes the input from the user and displays it on the screen but before using spread operator the More on stackoverflow.com
🌐 stackoverflow.com
Using Spread operator to update the state in react
Hi, does copying nested object using spread have different address (reference). We do that so react can know while rendering the dom that there is some change (because of the references are different). But the value we update remains the same since spread doesn’t work with nested keys? More on forum.freecodecamp.org
🌐 forum.freecodecamp.org
1
0
February 20, 2020
🌐
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....
🌐
GeeksforGeeks
geeksforgeeks.org › reactjs › what-is-the-meaning-of-spread-operator-in-reactjs
What is the Meaning of Spread Operator (...) in Reactjs? - GeeksforGeeks
July 23, 2025 - // For array expansion const array1 = [1, 2, 3]; const array2 = [...array1, 4, 5]; // [1, 2, 3, 4, 5] // Object Expansion const obj1 = { a: 1, b: 2 }; const obj2 = { ...obj1, c: 3 }; // { a: 1, b: 2, c: 3 } // Passing Props const childProps = { name: 'John', age: 25 }; <ChildComponent {...childProps} /> // Managing State this.setState((prevState) => ({ ...prevState, user: { ...prevState.user, name: "New Name" } })); The spread operator is very useful when you want to make an exact copy of an existing array, you can use the spread operator to accomplish this quickly. Step 1: Create a React application using the following command:
🌐
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 - Efficient State Updates: The spread operator allows you to create a new state object based on the previous state, which can be more efficient and lead to fewer re-renders compared to other methods.
🌐
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.
🌐
javascriptroom
javascriptroom.com › blog › spread-operator-in-react-setstate
Why Use the Spread Operator in React .setState()? (Even When the App Works Without It)
The spread operator ensures immutability, preserves nested state, avoids array mutation bugs, and makes state changes predictable—critical for maintainable, bug-free code. By adopting the spread operator in setState(), you’ll write code ...
Find elsewhere
🌐
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 - This ensures that React can accurately ... 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....
Top answer
1 of 2
1

The problem occurs when you call setError multiple times from validateSubmit. Only the last value will win - in your example that's the one that added "false": true (because errors.message that you used as a property name is false).

Notice that setError does not (synchronously, or at all) update the error constant, it only changes the component state and causes it to re-render with a new value. The {...errror, …} always did refer to the original value of error. To avoid this, you can

  • either aggregate the errors into a single value before calling setError once

    function validateSubmit(e) {
      let newError = error;
      if (!values.name) {
        newError = { ...newError, name: true };
      }
      if (!values.email) {
        newError = { ...newError, email: true };
      }
      if (!values.message) {
        newError = { ...newError, message: true };
      }
      console.log(error, newError);
      setError(newError);
      return newError != error;
    }
    
  • or use the callback version of setError that will execute the updates in a row and always pass the latest state in each callback as an argument:

    function validateSubmit(e) {
      let response = true;
      if (!values.name) {
        setError(oldError => ({ ...oldError, name: true }));
        response = false;
      }
      if (!values.email) {
        setError(oldError => ({ ...oldError, email: true }));
        response = false;
      }
      if (!values.message) {
        setError(oldError => ({ ...oldError, message: true }));
        response = false;
      }
      console.log(error);
      return response;
    }
    
2 of 2
1

The answer here is useReducer() to modify only portions of the state. https://reactjs.org/docs/hooks-reference.html#usereducer.

const errors = {
     name: false,
     email: false,
     message: false,
};

const reducer = (state, action) => {
    return {...state, ...action};
};

const [error, updateError] = useReducer(reducer,
    errors
);

function validateSubmit(e) {
    let response = true;
    if (!values.name) {
      updateError({name: true });
      response = false;
    }
    if (!values.email) {
      updateError({email: true });
      response = false;
    }
    if (!values.message) {
      updateError({message: true });
      response = false;
    }
    return response;
}
🌐
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 - const [state, setState] = useState({ name: "John", age: 30 }); const updateAge = () => { setState(prevState => ({ ...prevState, age: 31 })); }; You can use the spread operator to merge arrays, which can be useful when dealing with lists in React.
🌐
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 - The spread operator is the perfect tool for this job. It allows us to create new arrays and objects based on existing ones, without ever modifying the originals. This is especially critical when working with state in useState and useReducer hooks.
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 › @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 could be use to simplify passing react props down to a component. In react redux and reducer functions, spread operator are applied to compute new state without mutating state.
🌐
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 - This ensures that the original state object is not mutated while being updated. The spread operator can also be used in React to manage an array of items and update the state by adding new items while the previous state remains unchanged.
🌐
Stack Overflow
stackoverflow.com › questions › 68826619 › spread-operator-in-react-setstate-in-the-usestate-hook
reactjs - Spread operator in React .setState() in the useState Hook - Stack Overflow
... Sign up to request clarification or add additional context in comments. ... Save this answer. ... Show activity on this post. Spread syntax can be used when all elements from an object or array need to be included in a list of some kind.
🌐
DMC, Inc.
dmcinfo.com › latest-thinking › blog › id › 9668 › 5-great-uses-of-the-spread-operator-in-a-react-app
5 Great Uses of the Spread Operator in a React App | DMC, Inc.
December 30, 2025 - Probably the most common usage of the spread operator in a React app is in passing props down to a child component. So long as the object you’re spreading includes all the properties the child component requires, the child component will use those and ignore the extraneous ones.
🌐
freeCodeCamp
forum.freecodecamp.org › t › using-spread-operator-to-update-the-state-in-react › 351831
Using Spread operator to update the state in react - The freeCodeCamp Forum
February 20, 2020 - Hi, does copying nested object using spread have different address (reference). We do that so react can know while rendering the dom that there is some change (because of the references are different). But the value we u…
🌐
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....
🌐
Stack Overflow
stackoverflow.com › questions › 75796697 › using-previous-state-with-spread-operator-in-react
reactjs - Using previous State with spread operator in React - Stack Overflow
March 21, 2023 - The answer is the same. If you're updating state based on the current value, you should always use the functional update form. You can still use the spread operator, eg setItems((prev) => [...prev, { new: stuff }]).
🌐
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.