Deleting a middle attachment moves another file's progress i


Topic: Deleting a middle attachment moves another file's progress indicator

AdaFrontend asked a week ago

Each attachment in our upload list has a local progress bar. If I remove the second item while three files are uploading, the third row briefly displays the removed file's progress. The submitted filenames and request IDs remain correct.

The list currently uses its array index as the React key. Individual preview rows keep a little local state for their expanded details. Could this explain why the wrong progress value appears after removing an item?


Mateusz Lazaru staff answered a week ago

Yes. An index key identifies a position rather than an attachment. After a removal, React can reuse the row at that position for the next file, including its local state. Use the stable queue ID as the row key and keep progress indexed by that same ID.

Create the queue ID once when the file is added. Generating a new key during rendering would remount rows on every update and cause a different set of state problems.

return queue.map(item => (
  <AttachmentRow
    key={item.id}
    item={item}
    progress={progressById[item.id] ?? 0}
  />
));

https://react.dev/reference/react-dom/components/input


Please insert min. 20 characters.

FREE CONSULTATION

Hire our experts to build a dedicated project. We'll analyze your business requirements, for free.

Status

Answered

Specification of the issue
  • User: Free
  • Premium support: No
  • Technology: MDB React
  • MDB Version: MDB5 9.0.0
  • Device: Laptop
  • Browser: Firefox
  • OS: Windows 11
  • Provided sample code: No
  • Provided link: No