Itâs a common task in React to add an item to 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 add an item, you have to use Reactâs state management . We will be using Reactâs useState Hook, to keep this 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:
So far, the list is just a JavaScript variable and is not stateful yet. To modify it, for example to add an item to it, we need to make the list stateful by making use of Reactâs state and its useState Hook :
Now we have a stateful list and we are able to modify it. Letâs add an input field and a button, each with a handler function , which both deal with updating the input fieldâs state and eventually adding an item to the list:
Before we can add an item, we need to track the input fieldâs state, because without the value from the input field, we donât have any text to give the item which we want to add to our list. So letâs add some state management to this first:
We have made the input field a controlled element , because it receives its internal value from Reactâs state now. Next, whenever someone clicks the button, we can add the name entered into the input field as a new item to the list:
We are using object property shorthand initialization here, because the variable name equals the objectâs property name . Then we are using the state updater function to pass in the new list.
Adding an item works, but with some flaws. Two things are missing. First, we should clean up the input field. And second, we need to define an identifier id property for the item too, otherwise we would not have a stable key attribute for the JSX mapped list item anymore. I am using the uuid node package here, which you can install with npm install uuid :
Thatâs it. Rather than mutating the list, we keep it as an immutable data structure and therefore create a new list based on the old list and the new item. This is because the concat 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 added item is set as the new state and the component re-renders to display more items. Thatâs everything there is to know about adding an item to a list in React. But there is more â¦
For example, in our case everything happens in one component. What would happen if you wanted to add an item to the list from a child component? Letâs continue with splitting the component into multiple components. We need a callback handler to pass the functionality as destructured props in order to add an item:
Thatâs it. You can add an item from a child component whereas the list is managed as state somewhere further up in a parent component. Now, we will continue by replacing Reactâs useState with Reactâs useReducer Hook . The reducer hook can be used in React for complex state transitions. This is not the case for our state at the moment, but it may be of interest in one of your projects in the future. Letâs start by defining a reducer function for managing the stateful list:
Essentially, a reducer function takes a state and an action as input and returns a new state as output based on this information. In addition, it has a branch for each action type. In this case, there is only one action type and thus only one case in the switch to add an item. The logic to add the item to the list moved from our handler function into this reducer.
Next, we will replace the componentâs useState hook with a useReducer hook. This hook returns the state and a dispatch function as an 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 .
Finally, your state may be more than just one list. Often, you will have a more complex state object, and the list is only one property of this object. How would you add an item to a list in an object then? Letâs go through this example first with Reactâs useState Hook again. Letâs say that next to the list there is a boolean flag to either show or hide the list with 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 only on the list anymore, butit also needs to take the object into account:
Again, we access the list property from the object to concat a new item to the list based on the name state from the input field. 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 a property rather than using the list directly as state. The addition of the item to the list stays the same.
All of the presented examples for adding an item to a list in React can be seen in this GitHub repository . If you have any feedback about how to add items to lists in React, just ping me.