简体   繁体   English

尝试修复:无法在未安装的组件上执行 React state 更新

[英]Trying to fix: Can't perform a React state update on an unmounted component

I have the following code:我有以下代码:

const getAllSlidersUrl = "a url";
const adminID = "an admin id";
const adminlogintoken="a token";

const SliderContainer = () => {
    const [allSlides, setAllSlides] = useState([]);

    useEffect(
        () => {
            axios.post(
                getAllSlidersUrl,
                {
                    adminid: adminID,
                    token: adminlogintoken
                }
            )
                .then(
                    (response) => {
                        setAllSlides(response.data);
                    }
                )
                .catch(
                    (error) => {
                        alert(error);
                    }
                )

        }, []
    );

    return (
        //I have a button here
       //when I press button it executes the following:
       {
           allSlides.map(
                 item =>
                    <div key={item.slider_id} style={{ paddingTop: "10px", paddingBottom: "10px" }}>
                         <SliderComponent
                              adminId={adminID}
                              adminLoginToken={adminlogintoken}
                              sliderID={item.slider_id}
                              sliderImage={item.slider_image}
                              sliderEnText={item.slider_en_text}
                              sliderArText={item.slider_ar_text}
                              sliderEnButtonText={item.slider_en_button_text}
                              sliderArButtonText={item.slider_ar_button_text}
                              sliderEnButtonLink={item.slider_en_button_link}
                              sliderArButtonLink={item.slider_ar_button_link}
                              deleteSliderOnClick={deleteSliderHandler}
                              updateSliderToAllSlides={updatingSliderHandler}
                              />
                     </div>
                 )
       } 
   );
}

When I login to the page and press F12 (to give me the inspect tab),sometimes everything is fine and I get no warning but most of the times I get the following warning:当我登录页面并按 F12(给我检查选项卡)时,有时一切都很好,我没有收到任何警告,但大多数时候我收到以下警告:

Can't perform a React state update on an unmounted component. This is a no-op, but it indicates a memory leak in your application. To fix, cancel all subscriptions and asynchronous tasks in a useEffect cleanup function.

and whenever this warning is present and I press the button the application breaks and I get error: allSlides.map is not a function并且每当出现此警告并且我按下按钮时,应用程序就会中断并出现错误: allSlides.map is not a function

I tried adding return () => {} at the end of the useEffect hook, I also tried changing useEffect to the following:我尝试在useEffect钩子的末尾添加return () => {} ,我还尝试将useEffect更改为以下内容:

useEffect(
        () => {
            let isSubscribed = true;
            axios.post(
                getAllSlidersUrl,
                {
                    adminid: adminID,
                    token: adminlogintoken
                }
             )
             .then(
                (response) => {
                    if(isSubscribed){
                        setAllSlides(response.data);
                     }
                        
                }
             )
             .catch(
                 (error) => {
                     alert(error);
              }
           )
           return () => {isSubscribed = false}
    }, []
);

But in both cases where I tryied to resolve the error, it doesn't resolve it.但是在我尝试解决错误的两种情况下,它都没有解决它。

Note that for whatever reason (in all 3 cases) sometimes the web application doesn't break and everything works fine, but most of the times I do get the warning and then the error if I press the button.请注意,无论出于何种原因(在所有 3 种情况下),有时 web 应用程序都不会中断并且一切正常,但大多数情况下,如果我按下按钮,我会收到警告,然后是错误。

Check the reponse data type if it is of array type or not because the error states that map is not a function that means allSlides is not an array.检查响应数据类型是否为数组类型,因为错误指出map is not a function ,这意味着allSlides不是数组。

About the warning关于警告

try using ref to cancel fetch like this:尝试使用 ref 取消获取,如下所示:

const cancelFetch = React.useRef(false);


useEffect(
    () => {
        
        axios.post(
            getAllSlidersUrl,
            {
                adminid: adminID,
                token: adminlogintoken
            }
         )
         .then(
            (response) => {
                if(cancelFetch.current){

                   return;
                    
                 }
                 setAllSlides(response.data);
            }
         )
         .catch(
             (error) => {
                 alert(error);
          }
       )
       return () => {cancelFetch.current = true;}
}, []
);

A better way to cancel is using abort controller but this can do too.一个更好的取消方法是使用 abort controller 但这也可以。 If the warning is still there then maybe another component (parent) is getting unmounted.如果警告仍然存在,则可能正在卸载另一个组件(父级)。

暂无
暂无

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

相关问题 如何修复无法在未安装的组件上执行 React state 更新 - How to fix Can't perform a React state update on an unmounted component 修复“无法对卸载的组件执行 React 状态更新”错误 - Fix "Can't perform a React state update on an unmounted component" error 如何修复 React 警告:无法在未安装的组件上执行 React state 更新 - How to fix React WArning : Can't perform a React state update on an unmounted component React 警告:无法对卸载的组件执行 React 状态更新。 要修复,请取消所有订阅 - React Warning: Can't perform a React state update on an unmounted component. To fix, cancel all subscriptions React:无法使用 Formik 对未安装的组件执行 React state 更新 - React : Can't perform a React state update on an unmounted component with Formik 反应原生,无法对未安装的组件执行 React state 更新 - react native, Can't perform a React state update on an unmounted component React:无法在未安装的组件上执行 React state 更新 - React: Can't perform a React state update on an unmounted component React - 无法在未安装的组件上执行 React state 更新 - React - Can't perform a React state update on an unmounted component React Can't perform a React state update on an unmounted component error - React Can't perform a React state update on an unmounted component error React Spring “无法在未安装的组件上执行 React state 更新” - React Spring “Can't perform a React state update on an unmounted component”
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM