Answers for "useffect compare previous value to current"

0

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])
}
Posted by: Guest on June-14-2021

Code answers related to "useffect compare previous value to current"

Code answers related to "Javascript"

Browse Popular Code Answers by Language