📜  useffect 将以前的值与当前值进行比较 - Javascript 代码示例

📅  最后修改于: 2022-03-11 15:01:39.632000             🧑  作者: Mango

代码示例1
//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])
}