FAQ: The State Hook - Arrays in State

This community-built FAQ covers the “Arrays in State” exercise from the lesson “The State Hook”.

Paths and Courses
This exercise can be found in the following Codecademy content:

Learn React

FAQs on the exercise Arrays in State

There are currently no frequently asked questions associated with this exercise – that’s where you come in! You can contribute to this section by offering your own questions, answers, or clarifications on this exercise. Ask or answer a question by clicking reply (reply) below.

If you’ve had an “aha” moment about the concepts, formatting, syntax, or anything else with this exercise, consider sharing those insights! Teaching others and answering their questions is one of the best ways to learn and stay sharp.

Join the Discussion. Help a fellow learner on their journey.

Ask or answer a question about this exercise by clicking reply (reply) below!
You can also find further discussion and get answers to your questions over in #get-help.

Agree with a comment or answer? Like (like) to up-vote the contribution!

Need broader help or resources? Head to #get-help and #community:tips-and-resources. If you are wanting feedback or inspiration for a project, check out #project.

Looking for motivation to keep learning? Join our wider discussions in #community

Learn more about how to use this guide.

Found a bug? Report it online, or post in #community:Codecademy-Bug-Reporting

Have a question about your account or billing? Reach out to our customer support team!

None of the above? Find out where to ask other questions here!

I can not seem to get removeItem working. I have even used CA’s solution and it doesn’t work. I also haven’t been able to find much documentation for using the state setter with callback functions, which is frustrating trying to figure the actual solution out for myself.

const removeItem = (targetIndex) => {
    setCart((prev) => {
      return prev.filter((item, index) => index !== targetIndex);

full code here:

1 Like

Try this instead:
const removeItem = (targetIndex) => {
setCart((prev) => {
return prev.filter((item, index) => index !== targetIndex);
Or remove your semicolon

1 Like

Can someone please explain to me how this item in the ItemList gets passed around? I mean how does it know what item is clicked.

export default function ItemList({ items, onItemClick }) {
  const handleClick = ({ target }) => {
    const item = target.value;
  return (
      {items.map((item, index) => (
        <button className={buttonStyle} value={item} onClick={handleClick} key={index}>

I guess this onItemClick(item) is confusing for me…does the item get passed back to addItem?:

  const addItem = (item) => {
    setCart((prev) => [...prev, item]);

items is meant to contain a list of grocery items while onItemClick is meant to hold a reference to an event handling function. In GroceryCart.js, we import and then render ItemList components:
<ItemList items={produce} onItemClick={addItem} />
<ItemList items={pantryItems} onItemClick={addItem} />

In ItemList.js, the itemList function returns a div which consists of a series of buttons. We iterate over items using a variable called item and create buttons with attributes ... value={item} onClick={handleClick} ...

When a button is clicked, the handleClick event handling function is called. The handleClick function takes an event object as its parameter ({ target }). When a button is clicked, this event object contains lots of information about the button that was clicked. Using the statement const item = target.value;, we extract the information carried in the value attribute of the button (Recall we set value={item} when rendering our buttons via our map statement). So, suppose the “Carrots” button was clicked. This button will have a value of “Carrots” because of the attribute value={item}. By using const item = target.value;, we extract the value “Carrots” from the targeted button and assign this value to the const item in our handleClick function. In our last statement of the handleClick function namely onItemClick(item);, we make a call to a function. Recall the statement we used in the other file
<ItemList items={produce} onItemClick={addItem} />
onItemClick contains a reference to the addItem function. So, basically onItemClick(item); translates to the call addItem(item);