0

I have this code

I have some sections and inside it I have an item, number and button. How can I remove some specific item?

I'm rendering the sections as:

{sections.map((section, index) => (
        <Section
          section={section}
          key={section.id}
          addItem={(item) => addItem(index, item)}
          removeItem={(i) => removeItem(index, i)}
        />
      ))}

And in my Section component, I'm rendering it as:

{section.items.map((item, i) => (
        <>
          <h2>{item}</h2>
          <h3>{section.number[i]}</h3>
          <button onClick={() => removeItem(i)}>Remove</button>
        </>
      ))}

The remove function is in the Section's parent component and is:

const removeItem = (index, i) => {
    let filteredItem = sections[index].items.splice(i);
    let filteredNumber = sections[index].number.splice(i);

    setSections((prev) => ({ ...prev, filteredItem, filteredNumber }));
  };

When I click the remove button it says that sections.map is not a function. What am I doing wrong?

4
  • 1
    How did you define your section variable? Could it be that its empty or not an array before or after state change? What happens when you do optional chaining? Commented Apr 20, 2022 at 12:44
  • setSections((prev) => ({ ...prev, filteredItem, filteredNumber })); this makes your sections variable an object, its no longer an array even if it was previously an array, and object does not have map() function like an array, may be try [ ... ] instead of { ... } ? Commented Apr 20, 2022 at 13:01
  • share code sandbox code link instead of just the output link Commented Apr 20, 2022 at 13:03
  • First time sharing the code, so my bad hahahaha. I'll edit it Commented Apr 20, 2022 at 15:41

2 Answers 2

1

At glance looks like you have an error in your Section component. The code:

{section.items.map((item, i) => (
    <>
        <h2>{item}</h2>
        <h3>{section.number[i]}</h3>
        <button onClick={() => removeItem(i)}>Remove</button>
    </>
))}

should be:

{section.items.map((item, i) => (
    <>
        <h2>{item.title}</h2>
        <h3>{item.number[i]}</h3>
        <button onClick={() => removeItem(i)}>Remove</button>
    </>
))}

Because you're passing down item and you're trying to render everything in the item object and section.number should be item.number.

Sign up to request clarification or add additional context in comments.

1 Comment

Hey could you check the stackblitz code i posted up there?
1

I see your problem. In the code below when removing an element you are setting the sections to a single object not an array anymore. so map() doesn't exist on an Object. You have to convert it back into an Array.

const removeItem = (index, i) => {
let filteredItem = sections[index].items.splice(i);
let filteredNumber = sections[index].number.splice(i);
setSections((prev) => ({ ...prev, filteredItem, filteredNumber }));
};

Edit:

Upon further inspection of your code I see more errors.

In the remove item section in Section.js i see your trying to

const itemTarget = section.items[i];

That seems to be cause you are acting as is section is one object. but its an array that already has one section so you have to call it as follows for it to grab the items from the first (default) section.

const itemTarget = section[0].items[i];

This is the same with the filtered variable you will have to make sure when removing the item you are removing it from the correct section aswell.

3 Comments

I tried something like that, could you check the stackblitz code?
I checked. Check my update, this is where you have to start looking
Thanks for checking it! But the section on my Section component is the iterated object itself, that's why i thought it would be section.items. I tried this const itemTarget = section[0].items[i]; const filtered = section[0].items.filter((item) => item !== itemTarget); setSections((prev) => [...prev, { ...prev, items: filtered }]); And it says that cannot read properties of undefined (reading 'items') And i also tried instead of section, 'sections', that is the array defined on the parent component. When i tried with 'sections', it says that cannot read '0'

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.