Created
December 30, 2022 20:59
-
-
Save kivircik-parantez/293e059594180e1d240b6f4b4be18a00 to your computer and use it in GitHub Desktop.
Adjusting some state when a prop changes
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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