diff --git a/snippet_data/snippetList.json b/snippet_data/snippetList.json index 0e0629a02..de4cbca66 100644 --- a/snippet_data/snippetList.json +++ b/snippet_data/snippetList.json @@ -580,6 +580,23 @@ "hash": "c9a0c97f92439438fd49c5ef50742524aae8a162e144342956bcf92cf1ebf470" } }, + { + "id": "usePrevious", + "type": "snippetListing", + "title": "usePrevious", + "attributes": { + "text": "A hook that stores the previous state or props.\n\n- Create a custom hook that takes a `value`.\n- Use the `React.useRef()` hook to create a `ref` for the `value`.\n- Use the `React.useEffect()` hook to remember the latest `value`.\n\n", + "tags": [ + "hooks", + "state", + "effect", + "intermediate" + ] + }, + "meta": { + "hash": "df9f5b8fd3c06d02bfedcaa55d0b86a2b93b0d4c027726e056aefc1940a3c63b" + } + }, { "id": "useSSR", "type": "snippetListing", diff --git a/snippet_data/snippets.json b/snippet_data/snippets.json index 511f9758d..b7fe5e535 100644 --- a/snippet_data/snippets.json +++ b/snippet_data/snippets.json @@ -930,6 +930,33 @@ "authorCount": 2 } }, + { + "id": "usePrevious", + "title": "usePrevious", + "type": "snippet", + "attributes": { + "fileName": "usePrevious.md", + "text": "A hook that stores the previous state or props.\n\n- Create a custom hook that takes a `value`.\n- Use the `React.useRef()` hook to create a `ref` for the `value`.\n- Use the `React.useEffect()` hook to remember the latest `value`.\n\n", + "codeBlocks": { + "style": "", + "code": "const usePrevious = value => {\n const ref = React.useRef();\n React.useEffect(() => {\n ref.current = value;\n });\n return ref.current;\n}", + "example": "const Counter = () => {\n const [value, setValue] = React.useState(0);\n const lastValue = usePrevious(value);\n \n return (\n
Current: {value} - Previous: {lastValue}
\n \n