How to Force a React Component to Reload when a Variable Changes

A handy way to completely remount a component when a variable changes is to make that variable part of the key attribute of the component.

E.g.

const someVariable = 'foobar';
<Foo key={ `foo-component-${someVariable}` } />

4 thoughts on “How to Force a React Component to Reload when a Variable Changes”

Leave a Reply

Your email address will not be published. Required fields are marked *