useEffect监听的变量改变了不执行useEffect

useEffect监听了两个state:year, orgId,但是这两个变量改变了useEffect并没有随着运行

问题所在:useEffect监听的变量定义在useEffect下面

解决办法:把这两个state的声明挪到useEffect上面:

const TrainingStatistics = (props) => {    

    useEffect( () => { console.log('useEffect', year)}, [year, orgId] )    

    const [orgId, setOrgId] = useState('')    

    const [year, setYear] = useState(new Date().getFullYear())    

return xxx

}



改为:

const TrainingStatistics = (props) => {    

    const [orgId, setOrgId] = useState('')    

    const [year, setYear] = useState(new Date().getFullYear())    

    useEffect( () => { console.log('useEffect', year)}, [year, orgId] )    

    return xxx


推荐阅读更多精彩内容