top of page
Logo der Online Agentur mdwp

Spread Operator

React JS Spread Operator is denoted by three dots (...). It is a powerful and useful feature which has been adopted in JavaScript (ES6), and it is used in various scenarios such as copying an array or object, merging objects, or passing objects as function parameters.

When used in a function call, the spread operator unpacks elements in an array or object expressions into individual arguments to the function. When used in an object or array literals, it creates new array/object by copying properties from an existing object.

Here is a simple example of how the spread operator can be used in React to update a state object:

this.state = {
name: 'John',
age: 20,

// Later in code when updating state

this.setState(prevState => ({
age: 25

In this example, using `{...prevState}` makes a copy of the current state, and `age: 25` overrides the age property with a new value. This way, we're sure we're keeping all existing state properties and only updating what needs to be updated. This is crucial because in React, state updates should be treated as immutable.

bottom of page