Languages
[Edit]
EN

React - how to add / remove items from array in state (class component)

0 points
Created by:
jarlh
635

In this article, we would like to show you how to add and remove items from an array in the state in React.

Below example presents two functions:

  • handleAddItem that uses spread operator (...) to create a copy of array in the state, adds an item to it and then with setState() method updates the state value.
  • handleRemoveItem that uses filter() method to remove item with the last index from the array.

Runnable example:

Note:

Go to this article to see functional component example.

References:

Edit

Alternative titles

  1. React - how to update array in state (class component)
1
Donate to Dirask
Our content is created by volunteers - like Wikipedia. If you think, the things we do are good, donate us. Thanks!
Join to our subscribers to be up to date with content, news and offers.

ReactJS

React - add / remove items from array in state (class component)
Native Advertising
🚀
Get your tech brand or product in front of software developers.
For more information Contact us
Dirask - we help you to
solve coding problems.
Ask question.

❤️💻 🙂

Join