简体   繁体   English

当我使用 Async/Await 时,我收到以下警告:Can't perform a React state update on an unmounted component

[英]When i use Async/Await i get the warning of : Can't perform a React state update on an unmounted component

I have already solved this problem, but I don't know why it works... When I use Async/Await the error persists我已经解决了这个问题,但我不知道它为什么会起作用......当我使用 Async/Await 时,错误仍然存在

const [data, setData]= useState([])
const thisComponent = useRef(true)
useEffect(async()=>{

    const http = axios()
    const {data:{restaurants}}= await http.get('/restaurants')
    if(thisComponent.current){
        setData(restaurants)
    }

    return ()=>{
        thisComponent.current=false
    }
},[])

But when I use promises, it seems to work但是当我使用承诺时,它似乎有效

const [data, setData]= useState([])
const thisComponent = useRef(true)
useEffect(()=>{
    const http = axios()
    http.get('/restaurants').then(({data:{restaurants}})=>{
        if(thisComponent.current){
            setData(restaurants)
        }

    })
    
    return ()=>{
        thisComponent.current=false
    }
},[])

Personally, I think that in promises, an action persists asynchronously.. and it will return the data no matter if the component is still rendered.就个人而言,我认为在 Promise 中,一个动作是异步持续的。无论组件是否仍然呈现,它都会返回数据。 On the other hand, with Async/Await, the fetch of data will be interrupted if the component is not rendered anymore, please correct me if I am mistaken另一方面,使用 Async/Await,如果组件不再渲染,数据的获取将被中断,如果我弄错了,请纠正我

Do not manually change a React reference because React can reassign it.不要手动更改 React 引用,因为 React 可以重新分配它。 Create your own flag:创建自己的标志:

const [data, setData]= useState([])
const thisComponent = useRef(true)
useEffect(async()=>{
    let mounted = true

    const http = axios()
    const {data:{restaurants}}= await http.get('/restaurants')
    if(mounted){
        setData(restaurants)
    }

    return ()=>{
        mounted=false
    }
},[])

Or if you want to do things the right way, you can use AbortController :或者,如果您想以正确的方式做事,可以使用AbortController

https://medium.datadriveninvestor.com/aborting-cancelling-requests-with-fetch-or-axios-db2e93825a36 https://medium.datadriveninvestor.com/aborting-cancelling-requests-with-fetch-or-axios-db2e93825a36

暂无
暂无

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

相关问题 为什么我收到以下 React Native 警告:Can't perform a React state update on an unmounted component - Why am I getting the following React Native warning: Can't perform a React state update on an unmounted component 无法对卸载的组件警告执行React状态更新 - Can't perform a React state update on an unmounted component warning 警告:无法对未安装的组件执行 React state 更新 - Warning: Can't perform a React state update on an unmounted component 我得到错误:即使我创建了清理,也无法对未安装的组件执行 React state 更新 - I get Error: Can't perform a React state update on an unmounted component even though i created cleanup 构建一个反应应用程序但收到一条我找不到的错误消息:警告:无法在未安装的组件上执行 React state 更新 - Building a react app but getting an error message that I can't locate: Warning: Can't perform a React state update on an unmounted component 无法摆脱:警告:无法对未安装的组件执行 React 状态更新 - Can't get rid of: Warning: Can't perform a React state update on an unmounted component 警告:无法对未安装的组件执行 React state 更新。 如何使用do mount - Warning: Can't perform a React state update on an unmounted component. How to use did mount 当子功能组件重定向时,无法对父功能组件中的未安装组件警告执行 React 状态更新 - Can't perform a React state update on an unmounted component warning in Parent functional component when Child functional component redirects 添加等待标记以调度后,无法对未安装的组件执行反应 state 更新 - Can't perform a react state update on an unmounted component after adding await tag to dispatch 我不断收到 Can't perform a React state update on an unmounted component - I keep getting Can't perform a React state update on an unmounted component
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM