From 5a75ce186a43a94708b9c4f89a42971e9a073e18 Mon Sep 17 00:00:00 2001 From: Gokhan Kurt Date: Thu, 9 Sep 2021 00:14:02 +0300 Subject: [PATCH 1/3] RFC: Callback Ref Cleanup --- text/0000-callback-ref-cleanup.md | 199 ++++++++++++++++++++++++++++++ 1 file changed, 199 insertions(+) create mode 100644 text/0000-callback-ref-cleanup.md diff --git a/text/0000-callback-ref-cleanup.md b/text/0000-callback-ref-cleanup.md new file mode 100644 index 00000000..80f9c25b --- /dev/null +++ b/text/0000-callback-ref-cleanup.md @@ -0,0 +1,199 @@ +- Start Date: 2021-09-08 +- RFC PR: (leave this empty) +- React Issue: (leave this empty) + +# Summary + +Callback Refs should allow returning a cleanup function which will be called when the ref is removed or changed. + +# Basic example + +```jsx +function MyComponent(props) { + const logClicks = useCallback((node) => { + if (!node) return; + + const onClick = () => console.log('clicked!'); + + node.addEventListener('click', onClick); + + return () => { + node.removeEventListener('click', onClick); + }; + }, []) + + return