自定义hooks
function usePrevious(value) {
const ref = useRef();
useEffect(() => {
ref.current = value;
});
return ref.current;
}
使用
const Component = (props) => {
const {valueA, valueB} = props
const prevValue = usePrevious({valueA, valueB});
useEffect(() => {
if(prevValue.valueA !== valueA) {
...
}
if(prevValue.valueB !== valueB) {
...
}
}, [valueA, valueB])
}