简体   繁体   English

React Native - useState 设置数据更新较晚

[英]React Native - useState set data update late

So in my app, my button is supposed to set my data, then navigate into the next page.所以在我的应用程序中,我的按钮应该设置我的数据,然后导航到下一页。 The problem here is that the data is not updated fast enough (I think).这里的问题是数据更新速度不够快(我认为)。 The console show null (which is the initial data I set), then it navigate to the next page carrying the null data.控制台显示 null(这是我设置的初始数据),然后导航到带有 null 数据的下一页。 If I go back to the previous page then go to the next page again, it will carry the previous data that is not sent before.如果我 go 回到上一页然后 go 再到下一页,它会携带之前没有发送的之前的数据。 I tried setTimeout but I guess it's a wrong way to use setTimeout.我尝试了 setTimeout,但我想这是使用 setTimeout 的错误方法。

  const [foto, setFoto] = useState(null);

  const sendQUtest = () => {
    setFoto('QUtest');
    setTimeout(() => {
      console.log('assessmentTimeout: ' + foto);
    }, 5000);
    console.log('assessment: ' + foto);
    navigator.navigate('pembayaranAssessment', foto);
  };
  const sendTOPtest = () => {
    setFoto('TOPtest');
    console.log('assessment: ' + foto);
    navigator.navigate('pembayaranAssessment', foto);
  };

I wonder if there is anyway to wait for the data.我想知道是否还有等待数据的方法。 The solution I saw in others question is using useEffect, but in this one I need it after the onPress.我在其他问题中看到的解决方案是使用 useEffect,但在这个问题中,我在 onPress 之后需要它。

usestate is Asynchronous and you cannot really make an await function for it there is two approaches one of them is using useEffect hook like the following,you can have multiple useEffect in one function use useEffect to check when value changes and then navigate usestate 是异步的,你不能真正等待 function 因为它有两种方法,其中一种是使用useEffect挂钩,如下所示,你可以在一个 function 使用 useEffect 检查值何时更改然后导航

    const [foto, setFoto] = useState(null);
       useEffect(()=>{
//this will fire  at the beginning and on foto changing value
if(foto == null){
//will ignore first run
}else{ navigator.navigate('pembayaranAssessment', foto); 
}},[foto])

  const sendQUtest = () => {
    setFoto('QUtest');
    setTimeout(() => {
      console.log('assessmentTimeout: ' + foto);
    }, 5000);
    console.log('assessment: ' + foto);
    //navigator.navigate('pembayaranAssessment', foto);
  };
  const sendTOPtest = () => {
    setFoto('TOPtest');
    console.log('assessment: ' + foto);
   // navigator.navigate('pembayaranAssessment', foto);
  };

YES setstate will take some time to update , you can directly send data to your navigate function YES setstate 需要一些时间来更新,您可以直接发送数据到您的导航 function

navigator.navigate('pembayaranAssessment', 'TOPtest');

Try this way试试这个方法

 const sendQUtest = () => {
    setFoto('QUtest');
    setTimeout(() => {
      navigator.navigate('pembayaranAssessment', foto); // add here
    }, 200);
  };

  const sendTOPtest = () => {
    setFoto('TOPtest');
    setTimeout(() => {
      navigator.navigate('pembayaranAssessment', foto); // add here
    }, 200);
  };

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

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