【问题标题】:Warning: Cannot update a component from inside the function body of a different component in React Native警告:无法从 React Native 中不同组件的函数体内更新组件
【发布时间】:2022-01-13 09:16:03
【问题描述】:

我有调用所有数据函数的加载屏幕。我使用异步函数进行所有函数调用。

//NOTE: this screen loads all the data and store it in store so user will have a smother experience
const LoadingScreen = (props) => {
  const gotToHomeScreen = () => {
    props.navigation.replace("Home", { screen: HOME_SCREEN });
  };
  //NOTE: loading data here for home screen journey
  const getRequiredAPIDataInStore = async () => {
    GetAllFieldProp();
    GetAllSalaryAPIResponse();
    GetSalaryAPIResponse();
    let { spinnerStateForm101 } = GetForm101API();
    let { spinnerStateForm106 } = GetForm106API();
    GetMessagesCountAPI();
    GetMessagesAPI(props);
    GetAllFormAPIResponse();
    GetAllSpecificSalaryAPIResponse();
    let { spinnerStateMonthly } = GetMonthlyAbsenceAPI(props);
    let { spinnerStateWeekly } = GetWeeklyAbsenceAPI(props);

    if (
      spinnerStateMonthly &&
      spinnerStateWeekly &&
      spinnerStateForm106 &&
      spinnerStateForm101
    ) {
      gotToHomeScreen();
    }
  };

  getRequiredAPIDataInStore();

export default LoadingScreen;

但我收到了警告消息。

Warning: Cannot update a component from inside the function body of a different component.
at src/screens/loading-screen.js:19:26 in gotToHomeScreen
at src/screens/loading-screen.js:37:6 in getRequiredAPIDataInStore

如何解决这个警告信息?

【问题讨论】:

  • 我怀疑您收到这些警告的原因是因为您没有等待您的 async 函数。因此,您正在从加载屏幕导航到主屏幕,同时尝试在加载屏幕中设置状态。请参阅下面的答案以获得更好的解决方案

标签: reactjs react-native react-redux react-hooks


【解决方案1】:

这是我将采取的方法。

const Loading = () => {
 const [spinnerStateMonthly, setSpinnerStatMonthly] = useState(null);
 const [spinnerStateWeekly, setspinnerStateWeekly] = useState(null);
 const [spinnerStateForm106, setspinnerStateForm106] = useState(null);
 const [spinnerStateForm101, setSpinnerStateForm101] = useState(null);

 const gotToHomeScreen = () => {
   props.navigation.replace("Home", { screen: HOME_SCREEN });
 };

 useEffect(() => {
  // async callback to get all the data and set state
  (async () => {
   await GetAllFieldProp();
   await GetAllSalaryAPIResponse();
   await GetSalaryAPIResponse();
   const { spinnerStateForm101: local101  } = GetForm101API();
   const { spinnerStateForm106: local106 } = GetForm106API();
   setSpinnerStateForm101(local101);
   setSpinnerStateForm106(local106);

   await GetMessagesCountAPI();
   await GetMessagesAPI(props);
   await GetAllFormAPIResponse();
   await GetAllSpecificSalaryAPIResponse();
   const { spinnerStateMonthly: localMonthly } = GetMonthlyAbsenceAPI(props);
   const { spinnerStateWeekly: localWeekly } = GetWeeklyAbsenceAPI(props);
   setSpinnerStateMonthly(localMonthly);
   setSpinnerStateWeekly(localWeekly);
 })();
}, []);


// effect to check for what the state is and if all the states are satisfied 
// then go to the home screen
useEffect(() => {
     if (spinnerStateMonthly 
         && spinnerStateWeekly 
         && spinnerStateForm106 
         && spinnerStateForm101) {
           gotToHomeScreen();
     }
   }, [spinnerStateMonthly, spinnerStateWeekly, spinnerStateForm101, 
       spinnerStateForm106]);


};

【讨论】:

  • 我收到一条错误消息,即无效的挂钩调用。
猜你喜欢
  • 1970-01-01
  • 2020-11-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-06-16
  • 1970-01-01
  • 2020-08-05
  • 2021-04-07
相关资源
最近更新 更多