简体   繁体   English

在完成提取之前渲染React.js

[英]React.js render before fetch is done

Okay i have a problem that my app goes into an render before my fetch is done so it renders wrong 好的,我的问题是我的应用程序在提取完成之前进入了渲染状态,因此渲染错误

here is the code 这是代码

componentWillMount() {     
  fetch('http://localhost:8081/milltime/login?users='+this.state.email, {

})
    fetch("http://localhost:8081/milltime/getUsers")
      .then(res => res.json())
      .then(info => {
        this.setInfo(info);
          for (var i = 0; i < info['mta:getUsersResponse']['mta:users'].length; i++) {
            const user = {
              id: info['mta:getUsersResponse']['mta:users'][i]['mta:UserId'],
              username: info['mta:getUsersResponse']['mta:users'][i]['mta:UserLogin'],
              name: info['mta:getUsersResponse']['mta:users'][i]['mta:FullName']
          }

          this.addUser(user); 
        }
  }).then(function() {
            console.log("signed in to milltimes");
        }).catch(function() {
            console.log("need to sign in to milltime");
        });

}

setInfo(info) {
    this.setState({
      info: info,
  })
}
     render() {
    if (!this.state.info) {
      return (
              <MilltimeLogin email={this.state.email}/>
     );  
    } 

  return(

 <div className="usersTable">
   <Table striped bordered condensed responsive hover>
     <thead>
     <tr>
     <th/>
      <th className="title">Employees</th>
      <th/>
      </tr>
       <tr>
        <th>Id</th>
        <th>Full Name</th>
        <th>Username</th>
       </tr>
     </thead>
       <tbody>
          {
            Object
              .keys(this.state.Users)
              .map(key => <User key={key} index={key} details={this.state.Users[key]} onChange={this.props.onChange} />)
          }
       </tbody>
   </Table>
 </div>

  );
}
}

The problem here is that the second fetch /getUsers need a session key that the first fetch provides to work otherwise it will give an error that its missing session. 这里的问题是,第二次获取/ getUsers需要第一个获取提供的会话密钥才能正常工作,否则它将给出一个错误,即丢失了会话。 And because it for some reason doesnt get this session key it doesnt set info so the render function will go into the if statement and render MilltimeLogin component instead of the rest. 并且由于某种原因它没有获得此会话密钥,因此它没有设置信息,因此render函数将进入if语句并渲染MilltimeLogin组件,而不是其余部分。 Anyone know how to make sure the first fetch is all done so the second one get the session key. 任何人都知道如何确保第一次获取均已完成,以便第二次获取会话密钥。

EDIT if i reload the page one time it works but thats not how i want it 如果我一次重新加载页面,则进行编辑,但这不是我想要的方式

Fetch is asynchronous as you can read here so nothing assure you that the first fetch will be over when your second one is starting. 提取是异步的,您可以在此处阅读因此没有什么可以确保您的第二次提取开始时第一次提取将结束。
Maybe the await prefix can help you there (read this for more information) Hope this helps you in your research 也许await前缀可以在这里为您提供帮助(有关详细信息,请阅读内容)希望这对您的研究有所帮助

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

相关问题 在 React.js 中的 render return() 中显示获取结果 - Show fetch results in render return() in React.js 在 React.js 的功能组件中首次渲染之前调用 api - Call api before first render in functional component in React.js React.js - 是否可以在第一次渲染之前获取元素的大小? - React.js - Is it possible to get the size of an element before the first render? 如何在 React.js 中渲染页面之前等待 fetch 完成 - How to wait for fetch to complete before rendering page in React.js react.js - 在requestAnimationFrame上呈现 - react.js - Render on requestAnimationFrame 获取React.js无法正常工作 - Fetch in React.js not working 如何在 React js 中渲染功能组件之前获取数据 - How to fetch data before render functionnal component in react js React js Render 在 Fetch 获取值之前工作 - React js Render works before Fetch get value React.js,如何立即呈现用户动作(在等待服务器传播之前) - React.js, how to render users action instantaneously (before waiting for server propagation) 如何在渲染组件之前从 react.js 获取数据并且无法读取未定义的属性“包含”? - How to fetching data from react.js before render compontents and Cannot read property 'includes' of undefined?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM