简体   繁体   English

在 React 中使用 useEffect API 调用时出错

[英]Error when using useEffect API call in React

I am calling this api inside a context provider:我在上下文提供程序中调用此 api:

const url = 'http://127.0.0.1:8000/api/posts/'

const PostListContextProvider = (props) => {
    const [posts, setPosts] = useState([])

    useEffect(async () => {
        const {data} = await axios.get(url);
        setPosts(data)
    }, [posts]);

    return (
        <PostListContext.Provider value={ posts }>
            { props.children }
        </PostListContext.Provider>
    );
}

Upon consuming the context using useContext, this error occurs:使用 useContext 使用上下文时,会发生此错误:

react-dom.development.js:19710 Uncaught TypeError: destroy is not a function

What am I doing wrong?我究竟做错了什么?

ps.even though I am getting the error, I am still successfully fetching the data ps.即使我收到错误消息,我仍然可以成功获取数据

useEffect should not be async useEffect不应该是async

Do it like this:像这样做:

useEffect(() => {
        (async () => {
          const {data} = await axios.get(url);
          setPosts(data)
        })()
}, [posts]);

useEffect is supposed to return a function if it returns something, since you are defining the callback function to useEffect as a promise, what it essentially returns is a promise which is not what it expects. useEffect is supposed to return a function if it returns something, since you are defining the callback function to useEffect as a promise, what it essentially returns is a promise which is not what it expects.

In order to use a async function within useEffect you would write it like为了在 useEffect 中使用异步 function 你可以这样写

useEffect(() => {

   async function myFn() {
        const {data} = await axios.get(url);
        setPosts(data)
   }
   myFn();
}, [posts]);

暂无
暂无

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

相关问题 React API 调用 useEffect 仅在参数被硬编码时运行,而不是在使用 state 时运行 - React API call in useEffect runs only when parameter is hardcoded, not when using state 当没有这样的条件调用时,对 useEffect Hook 条件调用反应错误 - React error for useEffect Hook conditional call when there is no such conditional call 在 useEffect 之外 React API 调用 - React API call outside useEffect 在包含 useEffect 的组件上调用 API (React Native) 时,cleraInterval () 不起作用 - cleraInterval ()not working when on a component that contains useEffect to call the API (React Native) 在 React 的 useEffect() 挂钩中使用 setInterval() 之前尝试进行 API 调用 - Trying to make an API call before using setInterval() in useEffect() hook in React 调用和 API 在 useEffect 中反应原生? - Call and API inside useEffect in react native? 如何在 React useEffect 中防止不必要的 API 调用? - How to prevent unnecessary API call in React useEffect? 当第一次在 React 中使用 Axios 没有错误时进行第二次 api 调用 - Make second api call when there is no error on the first using Axios in React 在 React 中,当它是 useEffect 的依赖项而不触发另一个 API 调用时,我如何将 append 值转换为功能 state? - In React how can I append values to functional state when it is a dependency of useEffect without triggering another API call? 使用反应( useeffect )获取 json api - Fetching json api using react ( useeffect )
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM