Hooks in React

You are using the following code to extract previous props/state while using hooks(not custom hooks).

function Counter() {
 const [count, setCount] = useState(1);

 X

}

What can be used in place of X to complete it?

Options
1.const prevCountRef = useRef();
 useEffect(() => {
 prevCountRef.current = count;
const prevCount = prevCountRef.current
 });
2.const prevCountRef = useRef();
 useEffect(() => {
 prevCountRef.current = count;
 });
 const prevCount = prevCountRef.current;

3.function usePrevious(value) {
 const ref = useRef();
 useEffect(() => {
 ref.current = value;
 });
 return ref.current;
}

4.function usePrevious(value) {
 const ref = useRef();
 useEffect(() => {
 ref.current = count;
 });
 return ref.current;
}

Related Posts