useEffect如何知道是哪个deps触发的副作用?-灵析社区

素素数数

useEffect(()=>{ console.log('effect','触发对象:') },[a,b,c]) 如上代码,在`effectCallback`中,我如何知道是哪个deps触发的函数

阅读量:13

点赞量:0

问AI
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 */} ); }