Skip to content

Instantly share code, notes, and snippets.

@oney
Last active June 4, 2022 00:37
Show Gist options
  • Save oney/de9bdaca4cff85051c8fdba8fbddc883 to your computer and use it in GitHub Desktop.
Save oney/de9bdaca4cff85051c8fdba8fbddc883 to your computer and use it in GitHub Desktop.
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