简体   繁体   English

React - 组件渲染后更改的值

[英]React - Value changed after Component Rendering

I have been working on a food related React Native App and for the login I wanted to seperate the Stacks which are returned for normal Users and Restaurants.我一直在开发一个与食物相关的 React Native 应用程序,对于登录,我想分离为普通用户和餐厅返回的堆栈。

The Problem I am experiencing is that the returned value of restaurantUserChecker is different to what the 2nd UseEffect gets.我遇到的问题是restaurantUserChecker的返回值与第二个UseEffect得到的不同。 console.log('1', doc.exists) is true and therefore should return true. console.log('1', doc.exists)为真,因此应该返回真。 But console.log('2', val) is false which results in all users getting the same stack.但是console.log('2', val)是 false ,这会导致所有用户获得相同的堆栈。

  async function restaurantUserChecker(){
  const check = firestore().collection('restaurantUsers').doc(auth().currentUser.uid); 

    await (check.get()).then((doc) => {
    if (doc.exists) {
      console.log('1', doc.exists);
      return true;
    }
  }).catch((error) => console.log(error));

  return false;
}

  function MyStack() {

  const [restaurantUser, setRestaurantUser] = useState(false);

  useEffect(() => {
    console.log(auth().currentUser.uid);
    restaurantUserChecker().then((val) => {
      console.log('2', val);
      setRestaurantUser(val);
    }).catch((error) => console.log(error));
  }, []);

  if(restaurantUser){
    return <RestaurantHomeStack/>
  }

  return <DrawerNavigator />;
}

export default function App() {
  return (
    <SafeAreaProvider>
      <NavigationContainer>
        <MyStack/>
      </NavigationContainer>
    </SafeAreaProvider>
  );
}

I would appreciate all kind of help, as I have struggled for many hours on this problem.我将不胜感激各种帮助,因为我在这个问题上已经挣扎了好几个小时。

Your function restaurantUserChecker must return a Promise.您的 function restaurantUserChecker必须返回 Promise。

async function restaurantUserChecker() {
    return new Promise(async (resolve, reject) => {
        const check = firestore()
        .collection("restaurantUsers")
        .doc(auth().currentUser.uid);

    await check
        .get()
        .then((doc) => {
            if (doc.exists) {
                console.log("1", doc.exists);
                resolve(true);
            }
        })
        .catch((error) =>{ console.log(error); reject(error)});

    resolve(false);
    });
}

You aren't required to return a promise from restaurantUserChecker as the other answer suggests.正如其他答案所暗示的,您不需要从restaurantUserChecker返回 promise。 It is allowed to call then on an async function. then允许在async function 上调用。 The following changes to restaurantUserChecker should also perform as you intended.restaurantUserChecker的以下更改也应按您的预期执行。

async function restaurantUserChecker(){
  const check = firestore()
        .collection("restaurantUsers")
        .doc(auth().currentUser.uid);

  try {
     var doc = await check.get();
     if (doc.exists) {
        console.log('1', doc.exists);
        return true;
     }
  } catch(error) {
       console.log(error);
  }
  return false;
}

Both of your answers worked and now I understand what caused the issue.您的两个答案都有效,现在我了解导致问题的原因。 You're the heroes we don't deserve but need, if I'm allowed to quote Batman here.如果允许我在这里引用蝙蝠侠的话,你们是我们不配但需要的英雄。 Thanks a lot guys!非常感谢你们!

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

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