考虑代码:
function Comp5 () {
const [count, setCount] = useState(0)
const valRef = useRef(0)
useEffect(() => {
setCount(prevCount => prevCount + 1)
}, [])
valRef.current += 1
console.log('valRef_Current', valRef.current)
return <div>Comp5</div>
}
function App () {
return (
<div>
<Comp5 />
</div>
)
}
你的控制台输出是什么?我认为应该是: valRef_Current 1 valRef_Current 2 valRef_Current 3 valRef_Current 4
但真正的输出是: valRef_Current 1 valRef_Current 1 valRef_Current 2 valRef_Current 3
谁能帮我理解为什么?
提前致谢