我应该忽略此警告吗? (反应)

我有以下代码;

const updateMonsters = useCallback(() => {
    setMonsterObjects((prevMonsters) => 
        prevMonsters.map(({orientation, pos, walkingState, type}) => {
            const [offsetLeft, offsetTop] = orientationFactorMap[orientation];
            const nextCoordinates = [pos[0] + offsetLeft, pos[1] + offsetTop];
            const isValidPosition = isNewPositionValid(nextCoordinates, invalidPositions);
            const nextOrientation = isValidPosition ? orientation : oppositeOrientationMap[orientation];
            const nextWalkingState = isValidPosition ? walkingState + 2 : 2;
            const nextPos = isValidPosition ? nextCoordinates : [pos[0] - offsetLeft, pos[1] - offsetTop];
            return {
                orientation: nextOrientation,
                walkingState: nextWalkingState,
                pos: nextPos,
                type
            }
        })
    );
}, [invalidPositions]); //Dep will never change. 

const updatePlayerStatus = useCallback(() => {
    setPlayerHealth((prevHealth) => 
        didMonsterTouchDog(dogObject.pos, monsterObjects.map(({pos}) => pos)) ? prevHealth - 1 : prevHealth
    )
}, [dogObject.pos, monsterObjects, setPlayerHealth]); // Dep WILL always change. 

useEffect(() => {
    console.log(1);
    const timeout = setInterval(() => {
        updateMonsters();
        updatePlayerStatus();
    }, 500);
    return () => clearInterval(timeout);
}, [updateMonsters, updatePlayerStatus])

我的意图是在加载组件时运行updateMonsters()。独自一人,没有问题。当我尝试添加updatePlayerStatus()时,问题开始了。由于dogObject.pos / monsterObjects发生更改,因此每更改一次它都会再次触发useEffect。如果不将它们添加为依赖项,它会按我的预期工作,但是会向我发出警告。

./src/components/Level.js
  Line 93:8:  React Hook useCallback has missing dependencies: 'dogObject.pos' and 'monsterObjects'. Either include them or remove the dependency array  react-hooks/exhaustive-deps

编辑:DogObject:

const [dogObject, setDogObject] = useState({
    pos: dog.initialPosition,
    orientation: dog.initialOrientation,
    walkingState: 2
})
评论