简体   繁体   English

如何解决承诺将状态置为反应

[英]How do a resolve a promise to setstate in react

I am new to react and javascript and having trouble trying to retrieve a value from a promise so that it can be used for operations. 我是刚接触React和javascript的人,在尝试从Promise检索值时遇到麻烦,因此可以将其用于操作。 I have did some research and seen a lot of promise tutorials that can return a console log or run a function. 我进行了一些研究,看到了很多可以返回控制台日志或运行函数的Promise教程。 But I have yet to see one that can allow me to save to a const/var so I can use for other operations like a setstate. 但是我还没有看到一个可以保存到const / var的文件,因此可以用于其他操作,例如setstate。

I have tried a different ways to resolve a promise from an async function so I can do a setstate but they all failed, I have narrowed it down to 3 ways that I have tried which console logs the right information, but when I setstate it fails. 我尝试了其他方法来解决异步函数中的诺言,因此我可以执行setstate,但是它们都失败了,我将其范围缩小为尝试使用哪种控制台记录正确信息的3种方法,但是当我setstate失败时。

This is a sample of my react component 这是我的反应成分的一个例子

  state = {
    user: {}
  }



  getCurrentUser = async () => {
        // to save the user details if they are logged in
        const jwt = localStorage.getItem('token')
        return jwtDecode(jwt)
}


  componentDidMount() {

    // method 1
    // returns a promise instead of a value so setstate fails
    let user = this.getCurrentUser()
    console.log(user)
    this.setState({user: user})
    console.log(this.state)


    // method 2
    // trying to resolve a promise and return a value so I save to a variable and then setstate
    user = this.getCurrentUser()
    user = user.then((value) => {
      //console log prints out exactly what I need
      console.log(value)
      return value
    })
    console.log(user)

    this.setState({user: user})
    console.log(this.state)


    // method 3
    // trying to do setstate inside the promise also fails
    user = this.getCurrentUser()
    user.then((value) => {
    this.setState({user: value})
    })

    console.log(this.state)

  }

Thank you for any tips anyone might have on how to resolve this, or if I am misunderstanding concepts on async or promises. 感谢您提供任何关于如何解决此问题的技巧,或者如果我对异步或承诺的概念有误解。

setState is async operation, Where second parameter is callback function which is executed after setState function is performend. setState是异步操作,其中第二个参数是回调函数,在setState函数执行结束后执行。

you can do 你可以做

let user = this.getCurrentUser();
user.then(userData => {
    console.log(userData)
    this.setState({user: userData}, () => {
        console.log(this.state)
    })

})

I don't know exactly what you need, but this.setState takes second argument as a callback, so something like this will display the updated state this.setState知道您需要什么,但是this.setState将第二个参数用作回调,因此类似这样的内容将显示更新后的状态

this.setState({user: user}, () => console.log(this.state));

Also something like this should work: 同样这样的事情应该工作:

user = this.getCurrentUser()
    user = user.then((value) => {
      //console log prints out exactly what I need
      console.log(value)
      return value
    }).then((user) =>
    this.setState({user}, () => console.log(this.state))
);

And you should use await in your async function to wait the data. 并且您应该在async函数中使用await来等待数据。

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

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