简体   繁体   English

使用异步获取反应钩子

[英]React hooks with async fetch

I'm new to React hooks, but I'm trying to use a useEffect with a useCallback , but getting the notorious React Hook "useList" 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 钩子的新手,但我正在尝试将useEffectuseCallback一起使用,但是React Hook "useList" 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 "useList" 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 error. React Hook "useList" 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 Hooks。

Thie file holds the makeRequest:该文件包含 makeRequest:

function useConnections = () => {
    const makeRequest = React.useCallback(async (props) => {
        // Determine base url, determine headers here
        const response = fetch(url, options);

        return response;
    }

    return { makeRequest };
}

This file is my useListProvider :这个文件是我的useListProvider

function useListProvider = () => {
    const { makeRequest } = useConnections();

    const useList = React.useCallback(async (props) => {
        // makerequest is just a wrapper for fetch with a bunch of needed headers.
        const response = await makeRequest(props);

        return { body: response.body };
    }

    return { useList };
}

This is the rendered page:这是渲染的页面:

function MainPage() {
    const [isBusy, setIsBusy] = React.useStore(false);
    const { useList } = useListProvider();

    React.useEffect(() => {
        if (!isBusy) { useList(); setIsBusy(false); } // ERROR HERE!
    }, [useList]);

    return (
        <React.Fragment>
            IsBusy: {isBusy}
        </React.Fragment>
    );
}

Maybe I'm not getting it, but I only want to grab the useList data when the state says it's not busy.也许我没有得到它,但我只想在 state 说它不忙时获取 useList 数据。 However, doing it this way, I get the error listed above.但是,这样做,我得到上面列出的错误。 I understand I can't think of this the same way as Component classes, but how would you approach single and multiple renders from a callback?我知道我不能以与 Component 类相同的方式来考虑这一点,但是您将如何从回调中处理单个和多个渲染?

I'm not entirely sure what is happening here because I'm doing something similar in the useConnections, etc. and not getting the same error?我不完全确定这里发生了什么,因为我在 useConnections 等中做了类似的事情并且没有得到同样的错误?

The lint rule for hooks assumes that things which start with "use" are hooks.钩子的 lint 规则假定以“use”开头的东西是钩子。 Thus it thinks that useList is a hook and trying to enforce the rules of hooks on it.因此它认为 useList 是一个钩子,并试图在它上面强制执行钩子的规则 But it's not actually a hook, it's just a normal function, so you just need to give it a different name and the lint rule will be satisfied.但它实际上并不是一个钩子,它只是一个普通的 function,所以你只需要给它一个不同的名字就可以满足 lint 规则。

function useListProvider = () => {
    const { makeRequest } = useConnections();

    const callback = React.useCallback(async (props) => {
        const response = await makeRequest(props);

        return { body: response.body };
    }, [makeRequest])

    return { callback };
}

// elsewhere:

const { callback } = useListProvider();

React.useEffect(() => {
    if (!isBusy) { 
      callback(); 
      setIsBusy(false);
    }
}, [callback]);

Why is it not a hook?为什么不是钩子? Well, a hook is either one of the built in hooks, or a function that calls one of the built in hooks.好吧,钩子要么是内置钩子之一,要么是调用内置钩子之一的 function。 Your callback doesn't meet those criteria.您的回调不符合这些条件。 It was created by useCallback, but that just means it's a memoized function, not a hook.它是由 useCallback创建的,但这只是意味着它是一个记忆的 function,而不是一个钩子。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM