Skip to content
Permalink
Branch: master
Find file Copy path
Find file Copy path
Fetching contributors…
Cannot retrieve contributors at this time
30 lines (21 sloc) 751 Bytes

usePreviousValue

Accepts a variable (possibly a prop or a state) and returns its previous render's value.

Why? 💡

  • You want to keep track of the changes of your component state/props to decide whether it should update or not

Basic Usage:

import { useState } from 'react'; 
import { useInterval, usePreviousValue } from 'beautiful-react-hooks'; 

const TestComponent = () => {
   const [seconds, setSeconds] = useState(0);
   const prevSeconds = usePreviousValue(seconds);
   
   useInterval(() => setSeconds(1 + seconds), 1000);
   
   return (
     <DisplayDemo>
       <p>{seconds}s</p>
       <p>The previous value of the state 'seconds' was: {prevSeconds}</p>
     </DisplayDemo>
   );
};

<TestComponent />
You can’t perform that action at this time.