Created
October 14, 2021 10:55
-
-
Save DeVoresyah/79fd552b5fd74b4c009778c7183c5770 to your computer and use it in GitHub Desktop.
usePrevious React Hook
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import React, { useState } from "react"; | |
import usePrevious from "./usePrevious"; | |
const App = () => { | |
const [count, setCount] = useState(0); | |
const prevCount = usePrevious(count); | |
return ( | |
<> | |
<p>Count: {count} - Previous Count: {prevCount || 0}</p> | |
<button onClick={() => setCount(count + 1)}>Add</button> | |
</> | |
); | |
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { useEffect, useRef } from "react"; | |
export default (value) => { | |
const ref = useRef(); | |
useEffect(() => { | |
ref.current = value; | |
}); | |
return ref.current; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment