Skip to content

Instantly share code, notes, and snippets.

@Keraito
Last active June 7, 2022 21:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Keraito/efd3b734cb502f285777a299bda37696 to your computer and use it in GitHub Desktop.
Save Keraito/efd3b734cb502f285777a299bda37696 to your computer and use it in GitHub Desktop.
type OtherComponentProps = {
// ...
onChange?: (value: number) => void;
};
// -----
const Component = () => {
// We know for sure it's not nullable as we're initialising it, but TypeScript doesn't so we have to help it.
const changeValue = useCallback<NonNullable<OtherComponentProps["onChange"]>>(
(value) => {
if (value > 0) {
doSomethingWithTheValue(value);
}
},
[]
);
return <SomeOtherComponent onChange={changeValue} />;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment