Skip to content

Instantly share code, notes, and snippets.

@ljmotta
Created October 14, 2020 13:53
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 ljmotta/6ac056652a4bb5ce135c05143925d8d7 to your computer and use it in GitHub Desktop.
Save ljmotta/6ac056652a4bb5ce135c05143925d8d7 to your computer and use it in GitHub Desktop.
TodoListEnvelopeView Use Imperative Handle
import { useImperativeHandle } from "react";
export const TodoListEnvelopeView = React.forwardRef<TodoListEnvelopeViewApi, Props>((props, forwardedRef) => {
// ...
useImperativeHandle(
forwardedRef,
() => ({
setUser,
addItem: (item) => setItems([...items, { label: item, completed: false }]),
getItems: () => items,
markAllAsCompleted: () => setItems(items.map((item) => ({ ...item, completed: true }))),
}),
[items]
);
// ...
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment