Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save kivircik-parantez/293e059594180e1d240b6f4b4be18a00 to your computer and use it in GitHub Desktop.
Save kivircik-parantez/293e059594180e1d240b6f4b4be18a00 to your computer and use it in GitHub Desktop.
Adjusting some state when a prop changes
function List({ items }) {
const [isReverse, setIsReverse] = useState(false);
const [selection, setSelection] = useState(null);
// Better: Adjust the state while rendering
const [prevItems, setPrevItems] = useState(items);
if (items !== prevItems) {
setPrevItems(items);
setSelection(null);
}
// ...
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment