Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR JAVASCRIPT

useffect compare previous value to current

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

// Use it in useEffect
const Component = (props) => {
    const {receiveAmount, sendAmount } = props
    const prevAmount = usePrevious(receiveAmount);
    useEffect(() => {
        if(prevAmount.receiveAmount !== receiveAmount) {

         // process here
        }
    }, [receiveAmount])
}
Source by stackoverflow.com #
 
PREVIOUS NEXT
Tagged: #useffect #compare #previous #current
ADD COMMENT
Topic
Name
5+2 =