1

the tableData array gets live data from firebase, when data is changed in db, the table gets updated like it should, but the problem is, CrudPanel is stuck with the data it rendered the first time.

the entire table gets updated but when you press buttons for edit it shows old record.

<tbody>
                {this.state.tableData.map((rowdata, index) => {
                    return (
                        <tr key={index+1}>
                            <td style={{textAlign:'center'}}>{index+1}</td>
                            <td>{rowdata.key}</td>
                            <td>{rowdata.data.Fullname}</td>
                            <td>{rowdata.data.Phonenumber}</td>
                            <td>{rowdata.data.dateofbirth}</td>
                            <td><CrudPanel username={rowdata.key} record={rowdata.data}/></td>
                        </tr>
                    )
                })}
</tbody>

Whenever a new record is inserted or a record is deleted,

  • tableData is being updated successfully.
  • table rows are getting re-rendered correctly, except the CrudPanel.

crud panel takes the takes the row data and renders two button insert, update, when pressed opens up a modal...

but the point is, it does not render crudPanel when the tableData is changing.

enter image description here

zz44 was the 13th record, i added "yoyo" so now 13th record is yoyo but if i pressed the edit button it show data of zz44, because it was the 13th record when page loaded first time.

the rowdata binds to the td is getting re-rendered but how and why does it leave CrudPanel untouched?

2
  • 1
    I think the problem is with your index being used as key; If a record is inserted in between an existing list, the index being used as key will cause issues; can you try another unique value as key other than index? Commented Jun 18, 2022 at 21:34
  • I would have not gussed it in a million years, thank you very much. Commented Jun 18, 2022 at 21:46

2 Answers 2

1

The problem would be with the index being used as the key. When the elements of the list are updated especially in the middle (elements are added or removed from the list), the use of index as key will create problems, We should be referring to another unique value in the object of the list for it to work as intended

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

Comments

0

thanks to Shahin Nazer Answer, i got rid of the problem i declared a global variable and used it as a key and boom everything works normal.

var UniqeKeyVarible = 1;

{this.state.tableData.map((rowdata, index) => {
                        return (
                            <tr key={UniqeKeyVarible++}>
                                <td style={{textAlign:'center'}}>{index+1}</td>
                                <td>{rowdata.key}</td>
                                <td>{rowdata.data.Fullname}</td>
                                <td>{rowdata.data.Phonenumber}</td>
                                <td>{rowdata.data.dateofbirth}</td>
                                <td><CrudPanel username={rowdata.key} record={rowdata.data}/></td>
                            </tr>
                        )
                    })}

Comments

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.