Last active
June 4, 2022 00:37
-
-
Save oney/de9bdaca4cff85051c8fdba8fbddc883 to your computer and use it in GitHub Desktop.
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
import { withStable } from "react-with-stable"; | |
const SendButton = withStable( | |
["onClick"], // list 'onClick' as stable callback | |
({ onClick }) => ( | |
<button onClick={onClick}>Click</button> | |
) | |
); | |
function Chat() { | |
const [text, setText] = useState(''); | |
// 🎉 No need to wrap it | |
const onClick = () => { | |
sendMessage(text); | |
}; | |
return <SendButton onClick={onClick} />; | |
} | |
// vs. | |
const SendButton = React.memo( | |
({ onClick }) => ( | |
<button onClick={onClick}>Click</button> | |
) | |
); | |
function Chat() { | |
const [text, setText] = useState(''); | |
// 😟 Still have to write useEvent every time | |
const onClick = useEvent(() => { | |
sendMessage(text); | |
}); | |
return <SendButton onClick={onClick} />; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment