Created
December 30, 2022 23:07
-
-
Save kivircik-parantez/39bf14ffe0fa2b1176c10beedbb89712 to your computer and use it in GitHub Desktop.
Notifying parent components about state 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 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