reactjs - How to compare oldValues and newValues on React Hooks useEffect? - Stack Overflow

PHOTO EMBED

Thu Apr 28 2022 20:56:22 GMT+0000 (UTC)

Saved by @vishalrathore97 #javascript #react.js #useeffect

const Component = ({ receiveAmount, sendAmount }) => {
  const prevAmount = useRef({ receiveAmount, sendAmount }).current;
  useEffect(() => {
    if (prevAmount.receiveAmount !== receiveAmount) {
     // process here
    }
    if (prevAmount.sendAmount !== sendAmount) {
     // process here
    }
    return () => { 
      prevAmount.receiveAmount = receiveAmount;
      prevAmount.sendAmount = sendAmount;
    };
  }, [receiveAmount, sendAmount]);
};
content_copyCOPY

https://stackoverflow.com/questions/53446020/how-to-compare-oldvalues-and-newvalues-on-react-hooks-useeffect