0

I'm building this To Do App which has multiple heading and topics. The state of the To do's is following:
{ Big Heading: "Todo List", topics: [ { Heading: "What to do today", Topics: [ { topic: "Mow the lawn", desc: "Before mowing the lawn move the kids pool"} ... ] } ... ] }

I map the topics:

{Topic.topics.map((topics, j) =>
   <tr key={`${index}-${j}`}>
     <td>
       <textarea
        type="text"
        id={`topic${index}`}
        name="topic"
        defaultValue={topic.topic}
        onChange={event => handleInputChange(index, event, j)}
        ></textarea>
      </td>
   <td>
    <textarea
     style={{width: "100%"}}
     type="text"
     id={`desc${index}`}
     name="desc"
     defaultValue={topic.desc}
     onChange={event => handleInputChange(index, event, j)}
     ></textarea>
    <Button variant="danger" size="sm" onClick={() => deleteTopic(index, j)}>-</Button>
    <AddButton array={inputFields[index].topics} index={index} j={j} />
   </td>
</tr>

)}

I'm trying to remove some of the Topics with buttons very next to the topic on function deleteTopic(index, j) Index comes from higher map for the Topic and Heading.

const deleteTopic = (index, j) => {
    const values = [...inputFields]
    values[index].topics.splice(j, 1)
    setInputFields(values)
} 

THE PROBLEM When using function deleteTopic it sets inputFields with correct State ( removing the correct topic from correct array with index and j). But react Render deletes the last topic that can be seen even though it still remains in the state and the one that was deleted from the state is still shown on the To Do list.

If I change the <tr key="1"> IT WORKS! But then every row has same key and textarea unfocuses onChange. If someone more educated would know why it works when all keys are same but when theyr not it doesnt work.

5
  • Does this answer your question? Why console.log shows correct array but the state is wrong (ReactJS) Commented Jun 24, 2020 at 13:22
  • ^ To supplement, its because your key is based on the array index, and the array values change indexes when you delete an item (besides the last item). This causes react to update and unmount the wrong elements Commented Jun 24, 2020 at 13:23
  • Thanks Brian! You helped me to understand the key principle, but can you explain why does the first <textarea> unfocus after each keypress when the following <textarea> under same <tr> doesnt unfocus? Commented Jun 24, 2020 at 13:38
  • I'm not certain without being able to run an example and fully understand what the behavior is. Commented Jun 24, 2020 at 13:40
  • 1
    I found it out after long bug fix hours and 10 minutes into your answer I was using the topics.topics value as a key and it seemed to work for removing since every row had unique key the "Topic", but when I change the topic via <textarea> it changes the topics.topic value hence rendering new row with different key value! I found this out by seeing the DOM change on every key. All fixed now! Commented Jun 24, 2020 at 13:56

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.