Robin Pokorny
1 min readMar 20, 2019

--

Hi Oliver,

Thanks for your comment. I see the good parts of your solutions and in the happy case, it should work.

However, I can imagine two things that could go wrong. First, if the objects cease to exist, it will be still held in memory because of the Map. I agree that is not a problem for most apps and it is easy to solve with using the WeakMap instead of Map and generating an id instead of position.

The second is related but different. Imagine you perform some transformation to the data before passing it to your component, like computing a property:

const list = props.items.map(
item => ({
…item,
emphasised: item.size > 5
})
)

Then every time the list is rendered new objects are created. And there is no way for you to track which one is which. You would have to depend on the order or find an ID — which you could do inside React already.

Your solution is good when the list is not modified but maybe just filtered. Which is an improvement from the index as a key! 😉

Robin

--

--

Robin Pokorny
Robin Pokorny

Written by Robin Pokorny

Architecture & Tech Leadership — Speaks — Loves maths — Reads specs — Podcasts — Lives in Berlin — robinpokorny.com

Responses (1)