gpt4 book ai didi

reactjs - 检查 useEffect 中的哪个依赖项被更新

转载 作者:行者123 更新时间:2023-12-04 17:33:08 24 4
gpt4 key购买 nike

我有这个 useEffect钩:

useEffect(() => {
setTop(t => t + (controller.position.y * tileSize))
setLeft(l => l + (controller.position.x * tileSize))
}, [controller.position])

我希望它只在 position 时进行加法运算变化。如果 tileSize更改我只希望它进行乘法运算。

我试着把它放在两个 useEffect但后来我得到了 React Hook useEffect has missing dependencies警告:
useEffect(() => {
setTop(t => t + (controller.position.y * spriteSize))
setLeft(l => l + (controller.position.x * spriteSize))
}, [controller.position])

useEffect(() => {
setTop((controller.position.y * spriteSize))
setLeft((controller.position.x * spriteSize))
}, [spriteSize])

在这种情况下,最佳做法是什么?

编辑:

一个可重现的例子:
const [tileSize, setTileSize] = useState(0)
const controller = {
position: {
x: 0,
y: 0
}
}
useEffect(() => {
setTop(t => t + (controller.position.y * tileSize))
setLeft(l => l + (controller.position.x * tileSize))
}, [controller.position])

useEffect(() => {
setTop((controller.position.y * tileSize))
setLeft((controller.position.x * tileSize))
}, [tileSize])

const asdf = () => {
setTileSize(150)
}

return (
<div onClick={() => asdf()}>click me</div>
)

警告信息:

Line 31: React Hook useEffect has a missing dependency: 'tileSize'. Either include it or remove the dependency array. You can also replace multiple useState variables with useReducer if 'setTop' needs the current value of 'tileSize' react-hooks/exhaustive-deps

Line 36: React Hook useEffect has missing dependencies: 'controller.position.x' and 'controller.position.y'. Either include them or remove the dependency array react-hooks/exhaustive-deps Line 46: Unreachable code no-unreachable

最佳答案

有很多方法,您应该查看最适合您的用例的一种。

  • 正如警告所暗示的那样,替换为 useReducer

  • React Hook useEffect has a missing dependency: 'tileSize'. Either include it or remove the dependency array. You can also replace multiple useState variables with useReducer if 'setTop' needs the current value of 'tileSize'. (react-hooks/exhaustive-deps)


    const STATE = {
    ADDITION: 'addition',
    MULTIPLICATION: 'multiplication'
    };

    function reducer(state, action) {
    switch (action.type) {
    case STATE.ADDITION:
    return {
    ...state,
    position: action.position,
    top: state.top + action.position * state.spriteSize
    };
    case STATE.MULTIPLICATION:
    return {
    ...state,
    spriteSize: action.spriteSize,
    top: state.position * action.spriteSize
    };
    default:
    throw new Error();
    }
    }

    function Controller({ position, spriteSize }) {
    const [state, dispatch] = useReducer(reducer, {
    top: 0,
    position,
    spriteSize
    });

    useEffect(() => {
    dispatch({ type: STATE.ADDITION, position });
    }, [position]);

    useEffect(() => {
    dispatch({ type: STATE.MULTIPLICATION, spriteSize });
    }, [spriteSize]);

    return <FlexBox>{state.top}</FlexBox>;
    }
  • 您可以使用引用 useRef并与旧值进行比较
  • 如果您知道自己在做什么,只需禁用 lint 警告。

  • Edit styled-antd-react-starter

    关于reactjs - 检查 useEffect 中的哪个依赖项被更新,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57843551/

    24 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com