import { useState, useEffect, useRef } from 'react';
function MyComponent() {
const [a, setA] = useState(0);
const [b, setB] = useState(0);
const [c, setC] = useState(0);
const prevA = useRef(a);
const prevB = useRef(b);
const prevC = useRef(c);
useEffect(() => {
if (prevA.current !== a) {
console.log('a has changed');
}
if (prevB.current !== b) {
console.log('b has changed');
}
if (prevC.current !== c) {
console.log('c has changed');
}
// 更新引用值,用来下次对比
prevA.current = a;
prevB.current = b;
prevC.current = c;
}, [a, b, c]);
return (
{/* some UI and buttons to change state */}
);
}