Javascript SetState不接受数字

Javascript SetState不接受数字,javascript,reactjs,setstate,Javascript,Reactjs,Setstate,我是新手,不明白为什么在componentDidMount上设置状态时会出现以下错误。我对类似的错误进行了研究,我知道react想要的是一个数字,而不是一个承诺。但是,我非常确定我是通过编写.then(res=>res.json().then(data=>data)来解析一个数字的。所以我不知道错误来自哪里 Uncaught Error: Objects are not valid as a React child (found: [object Promise]). If you meant

我是新手,不明白为什么在componentDidMount上设置状态时会出现以下错误。我对类似的错误进行了研究,我知道react想要的是一个数字,而不是一个承诺。但是,我非常确定我是通过编写
.then(res=>res.json().then(data=>data)
来解析一个数字的。所以我不知道错误来自哪里

Uncaught Error: Objects are not valid as a React child (found: [object Promise]). If you meant to render a collection of children, use an array instead.
    in p (created by Info)
    in div (created by Info)
    in Info
这是我用来获取数字的函数:

const get_eth = wallet =>
  fetch(
    `https://api.etherscan.io/api?module=account&action=balance&address=${wallet}&tag=latest`
  )
    .then(res =>
      res.json().then(data => data["result"] / 10 ** 18)
    )
    .catch(err => console.log(err));
这是设置状态:

  componentDidMount() {
    this.setState({eth_q: get_eth("0x0975ca9f986eee35f5cbba2d672ad9bc8d2a0844")})
  }
我想你想要:

const get_eth = wallet =>
  fetch(API)
    .then(res => res.json())
    .then(data => data["result"] / 10 ** 18)
    .catch(err => console.log(err));
也就是说,您正在将
.then()
链接到您的
.json()
,而不是链接到您的
fetch()

然后,在您的
组件didmount()
中:


为什么不在promise resolve?中执行setState的可能重复。然后(res=>res.json()。然后(data=>this.setState({eth_q:data[“result”]/10**18))。在React中,只要更新呈现触发器的状态,就可以随时进行响应!
componentDidMount() {
  get_eth('0x0975ca9f986eee35f5cbba2d672ad9bc8d2a0844').then(res => {
    this.setState({ eth_q: res });
  });
}