If you're mapping over an array to render elements and in the resulting array of elements you have a null
and then 2 or more items with the same key, on each re-render, React will add another element to the DOM like those two.
Here's a minimal code for reproducing:
const LIST = [{ id: 2, done: true },{ id: 1 },{ id: 1 }];
function App() {
const forceUpdate = React.useState()[1];
return LIST.map(({ id, done }, i) => {
if (done) {
return null;
}
return (
<p key={id} onMouseEnter={forceUpdate}>
Index: {i}
</p>
);
});
}
And here's that same code on CodeSandbox for playing with it: https://codesandbox.io/s/8kqxr
Notice that when you hover over the list (and therefore cause re-render), React adds an additional element to the resulting DOM.
The issue can be avoided by either of these:
ensuring unique id
moving all
null
elements to the end of the listensuring there are no
null
elements
I know React is NOT supporting elements with the same key and is warning that using those can result in errors, but I still wonder what exactly causes this? Can someone with an in-depth knowledge of JSX and React reconciliation explain it to me?