Skip to content

.tip

· one of the small things

Change the key to reset a component's state

If a form keeps showing the previous user's draft after you navigate to the next user, the state lives in a component React reused. Give it a key that changes with the identity.

<EditUserForm key={user.id} user={user} />

A new key unmounts the old instance and mounts a fresh one. That's the intended way to reset state, and it's one prop instead of an effect copying props into state.

react