useeffect previous state and next state code example
Example 1: useeffect previous state
const Component = (props) => {
const {receiveAmount, sendAmount } = props
const usePrevious = (value) => {
const ref = useRef();
useEffect(() => {
ref.current = value;
});
return ref.current;
}
const prevAmount = usePrevious({receiveAmount, sendAmount});
useEffect(() => {
if(prevAmount.receiveAmount !== receiveAmount) {
}
if(prevAmount.sendAmount !== sendAmount) {
}
}, [receiveAmount, sendAmount])
}
Example 2: previous state in useEffect
function usePrevious(value) {
const ref = useRef();
useEffect(() => {
ref.current = value;
});
return ref.current;
}
const Component = (props) => {
const {receiveAmount, sendAmount } = props
const prevAmount = usePrevious({receiveAmount, sendAmount});
useEffect(() => {
if(prevAmount.receiveAmount !== receiveAmount) {
}
if(prevAmount.sendAmount !== sendAmount) {
}
}, [receiveAmount, sendAmount])
}