简体   繁体   English

如何在本机反应中处理异步 function 响应并保存在 useState 中?

[英]How to handle Async function response in react native and save in useState?

const [confirm, setConfirm] = useState(null);

const signIn = async phoneNumber => {
  console.log('Entered into signIn Function');
  setTextInputState(false);
  setSendOtpButtonState(true);

  const confirmation = await auth().signInWithPhoneNumber(phoneNumber);
  console.log('This is conformation1' + JSON.stringify(confirmation));
  setSendOtpButtonState(true);
  setOtpInputState(true);

  let res = confirmation;
  setConfirm(res);

  console.log('This is conformation2' + JSON.stringify(confirm));
};

How to set conformation in the useState.如何在 useState 中设置构象。 when we enter into the signIn function for the first time getting useState value as null and for the second time setting the value in the useState.当我们第一次进入 signIn function 时,将 useState 值设为 null,第二次在 useState 中设置该值。 i want to set value for the first time.我想第一次设置值。

This might help这可能会有所帮助

const signIn = async (phoneNumber) => {

  const [confirm, setConfirm] = useState(null);
  ...

  useEffect(async () => {
    const confirmation = await auth().signInWithPhoneNumber(phoneNumber);
    console.log("This is conformation1" + JSON.stringify(confirmation));
    setSendOtpButtonState(true);
    setOtpInputState(true);

    let res = confirmation;
    setConfirm(res);
  }, []);

  console.log("This is conformation2" + JSON.stringify(confirm));
};

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

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