Itâs a common task in React to update an item in a list. Here I want to show you briefly how this works. Every time you want to modify something in React, for example a list where you want to change an item, you have to use Reactâs state management . We will be using Reactâs useState Hook here, for the sake of keeping the first example simple, however, you can also use Reactâs useReducer Hook, as you will see later.
We will start with a typical list in React where we provide a stable key attribute for each rendered list item:
In addition, the list item is either struck through or untouched based on its isComplete boolean flag. We are using inline style for quick prototyping here:
So far, the list is just a JavaScript variable and not stateful yet. In order to modify it, in this case to edit an item in it, we need to make the list stateful with Reactâs state and its useState Hook :
Now we have a stateful list and we are able to alter it. Letâs add a button with a handler function which deals with the click event for each item in the list. In this case, the button should be there for editing an item:
Since we are in a mapped list, we need to figure how to pass the specific item, or the itemâs identifier, which we want to change in the list, to the handler function. The most straightforward approach to this would be using an inline handler to sneak in the item, or item identifier in this case, as a parameter:
The only thing missing is updating the specific item in the list whenever a click on the button happens. We will do this by modifying the current stateful list with a map function:
Rather than mutating the list, we keep it as immutable data structure and therefore create a new list based on the mapped list where we change every item which meets the condition. If an item meets the condition, we are using all the itemâs properties for the new item with JavaScriptâs spread operator and change the property that we want to modify. Itâs because the map function doesnât modify the list but only returns a new list.
Now, when our state updater function from Reactâs useState Hook is called, the list with the changed item is set as new state and the component re-renders to display all items again. Thatâs everything there is to know about changing an entry in an array in React. But there is more â¦
For example, in our case everything happens in one component. What would happen if you would want to update an item from the list from a child component? Letâs continue with splitting the component into multiple components. We will need a callback handler to pass the functionality as destructured props in order to change an item:
Thatâs it. You are able to update an item from a child component whereas the list is managed as state somewhere up in a parent component. If you would want to manage the list as state in the List component instead of managing it in the App component, you would have to lift state .
Now, we will continue by exchanging Reactâs useState with Reactâs useReducer Hook . The reducer hook can be used in React for complex state and complex state transitions. This is not the case for our state at the moment, but it could be of interest for your particular case in the future. Letâs start by defining a reducer function for managing the stateful list:
Essentially a reducer function takes a state and action as input and returns a new state based on this information as output. In addition, it has a branch for each action type. In this case, there is only one action type and thus one branch to edit an item. The actual logic to update the item from the list moved from our handler function into this reducer now.
Next, we will exchange the componentâs useState hook with a useReducer hook. This hook returns the state and a dispatch function as array which we conveniently access again via array destructuring . The dispatch function is then used in our handler function by passing an appropriate action to it:
Thatâs it for using useReducer instead of useState. Both state hooks are useful in React, so you should decide based on your needs whether you need a useReducer or useState hook .
Last but not least, it may not always be the case that your state is only the list. Often you will have a more complex state object and the list is only one property of this object. How would you change an item from this list in the object then? Letâs go through this example first with Reactâs useState Hook again. Letâs say next to the list there is a boolean flag to either show or hide the list with a conditional rendering :
We start off with a complex state object which has the list as one of its properties. Wherever we want to use the list (or the boolean flag), we need to access the property from the object first. The only thing missing is fixing the handler function, because it cannot operate solely on the list anymore, but needs to take the object into account:
Again, we access the list property from the object to edit the list item based on the incoming identifier. Then, we have to update the state with the complex state object again. We could set both, the new list and the boolean flag â which didnât change â explicitly, but in this case we are using JavaScriptâs spread operator to spread all key/value pairs from the state object into the new state object while overriding the list property with the new list. Letâs apply the same technique for the example with the reducer function:
Thatâs it. Similar to the previous version, we are just applying all the changes to the complex state object which has the list as property rather than using the list directly as state. The updating of the item in the list stays the same.
All the shown examples for changing an item in a list in React can be seen in this GitHub repository . If you have any feedback about how to update items in lists in React, just ping me.