首页 > 解决方案 > 在反应中一个接一个地运行功能

问题描述

之后setState,我想调用service.logout(),然后Auth.logout()函数,

 componentWillMount() {
    if (!(this.Auth.loggedIn())){
      this.props.history.replace('/login');
    } else {
      this.setState({token : this.Auth.getToken()}).then(service.logout(this.state.token)).then( this.Auth.logout())
      // console.log('token',this.state.token)

    }
  }

但我变得error这样了,

 TypeError: Cannot read property 'then' of undefined
Logout.componentWillMount
src/components/auth/Logout.js:20
  17 | if (!(this.Auth.loggedIn())){
  18 |   this.props.history.replace('/login');
  19 | } else {
> 20 |   this.setState({token : this.Auth.getToken()}).then(service.logout(this.state.token)).then( this.Auth.logout())
  21 |   // console.log('token',this.state.token)
  22 |  
  23 | }

标签: javascriptreactjsreact-nativereact-redux

解决方案


https://reactjs.org/docs/

在此处输入图像描述

所以我认为这会解决你的问题

      this.setState({token : this.Auth.getToken()} , async () => {
       await service.logout(this.state.token) 
       await this.Auth.logout()
      });
      
      
      


推荐阅读