Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save kivircik-parantez/39bf14ffe0fa2b1176c10beedbb89712 to your computer and use it in GitHub Desktop.
Save kivircik-parantez/39bf14ffe0fa2b1176c10beedbb89712 to your computer and use it in GitHub Desktop.
Notifying parent components about state changes
function Toggle({ onChange }) {
const [isOn, setIsOn] = useState(false);
function updateToggle(nextIsOn) {
// ✅ Good: Perform all updates during the event that caused them
setIsOn(nextIsOn);
onChange(nextIsOn);
}
function handleClick() {
updateToggle(!isOn);
}
function handleDragEnd(e) {
if (isCloserToRightEdge(e)) {
updateToggle(true);
} else {
updateToggle(false);
}
}
// ...
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment