Pretty sure I'm missing something fundamental about React.
So here's the code:
export function EditTagsModal({existingTags}) {
console.log('existingTags', existingTags);
const [selectedTags, setSelectedTags] = React.useState(existingTags);
console.log('selectedTags:', selectedTags);
...
}
In a parent component, <EditTagsModal />
is consumed like this:
{tags && <EditTagsModal existingTags={tags} /> }
Here is how it goes down:
existingTags
starts with, say,['hello']
setSelectedTags
is doing its thing.selectedTags
is now['hello', 'world', 'world']
.- Then I send tags to server, close modal, parent re-render.
- Now the
tags
returned by server is['hello', 'world']
, without the duplicates. - Here's the thing: Now I open the modal again, I'm still seeing my values in
selectedTags
(in that second console.log above) that has the duplicates, not thetags
(the first console.log) from my server.
What's up with react?