简体   繁体   English

如何禁用 React Hook “useCallback”不能在回调警告中调用?

[英]How to disable React Hook “useCallback” cannot be called inside a callback warning?

I have created a utility hook that allow updating the state of components as long as they are mounted.我创建了一个实用程序挂钩,只要它们被安装,它就允许更新组件的状态。

to do so, I had to call useCallack inside the callback function of Array.map为此,我必须在useCallack的回调函数中Array.map
this is my code这是我的代码

export const useSafeDispatches = (...dispatches) => {
    const mounted = useRef(false);
    useLayoutEffect(() => {
        mounted.current = true;
        return () => (mounted.current = false);
    }, []);

    return dispatches.map((dispatch) =>
        useCallback((...args) => (mounted.current ? dispatch(...args) : void 0), [dispatch])
    );
};

I am getting this error when I try to build React Hook "useCallback" cannot be called inside a callback. React Hooks must be called in a React function component or a custom React Hook function react-hooks/rules-of-hooks当我尝试构建React Hook "useCallback" cannot be called inside a callback. React Hooks must be called in a React function component or a custom React Hook function react-hooks/rules-of-hooks时出现此错误React Hook "useCallback" cannot be called inside a callback. React Hooks must be called in a React function component or a custom React Hook function react-hooks/rules-of-hooks React Hook "useCallback" cannot be called inside a callback. React Hooks must be called in a React function component or a custom React Hook function react-hooks/rules-of-hooks

The error message says exactly what you need to do, pull useCallback out of the map callback.错误消息准确地说useCallback您需要做什么,将useCallbackmap回调中拉出。 Your callback depends on the value of mounted.current , so we make sure to include it in our list of dependencies -您的回调取决于mounted.current的值,因此我们确保将其包含在我们的依赖项列表中 -

export const useSafeDispatches = (...dispatches) => {
    
    const mounted = useRef(false);

    useLayoutEffect(() => {
        mounted.current = true;
        return () => (mounted.current = false);
    }, []);

    const safeDispatch = useCallback(dispatch =>
      (...args) => mounted.current ? dispatch(...args) : void 0
    , [mounted.current]);

    return dispatches.map(safeDispatch);
};

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 无法在回调内部调用React Hook“ useInterval” - React Hook “useInterval” cannot be called inside a callback 无法在回调中调用 React Hook "useCategory" - React Hook "useCategory" cannot be called inside a callback 不能在回调中调用 React Hook “useState” - React Hook “useState” cannot be called inside a callback 不能在回调中调用 React Hook “useLocation” - React Hook "useLocation" cannot be called inside a callback “useState”不能在回调中调用。 如何在“useCallback”上使用 state? - “useState” cannot be called inside a callback. How to use state on “useCallback”? 如何使用react修复错误“react use hook cannot be called inside a callback function”? - How to fix the error "react use hook cannot be called inside a callback function" using react? 错误:无法在回调中调用 React Hook“useLogout”。 阿波罗 - Error: React Hook "useLogout" cannot be called inside a callback. Apollo React useCallback 设置依赖于在回调中调用的 function - React useCallback setting dependency on a function called inside the callback 如何根据另一个请求的响应向 API 发出请求? 不能在回调中调用 React Hook “useSwr” - How can I make a request to an API based on the response from another request? React Hook “useSwr” cannot be called inside a callback 在 React useCallback hook 中,回调函数中的参数从哪里来 - In React useCallback hook, From where does the parameters come inside the callback function
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM