previous state shows on react hooks 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: usestate access previous state
const [arrayOfObjs, handleObjSelection] = useState([]);
<button
onClick={selectedObj => handleObjSelection(
prevSelected => [...prevSelected, selectedObj],
))}
>