简体   繁体   English

为什么我的React Text组件没有显示文本? (反应360)

[英]Why is my React Text component showing no text? (React 360)

I have the following code in React: 我在React中有以下代码:

  export class CryptoInformation extends React.Component {


  state = {
    cryptoInformation: {}
  }

  componentDidMount() {
    fetch('https://api.coinmarketcap.com/v2/ticker/1/')
    .then(response => response.json())
    .then(response => {
      this.setState({cryptoInformation: response["data"]});
    })
    .then(response => {

      #########This logs to the console 'Bitcoin'#######
      console.log(this.state.cryptoInformation.name);


    })
  }

  render() {
    return (
      <View>

        ######## Why is this not showing 'Bitcoin' #######
        <Text>{this.state.cryptoInformation.name}</Text>
      </View>
    )
  }
}

The API works perfectly and I am able to get the information I need to be stored in my cryptoInformation object. 该API可以正常运行,并且我能够获取需要存储在我的cryptoInformation对象中的信息。 However, when I try to display the name portion of the JSON, I get nothing for the Text. 但是,当我尝试显示JSON的name部分时,Text没有任何显示。 Why is this so? 为什么会这样呢? This should be straightforward. 这应该很简单。 Am I presenting the data wrong? 我输入的数据有误吗? Help would be appreciated. 帮助将不胜感激。 Thanks. 谢谢。

----Edit---- - - 编辑 - -

The data that is returned has the following structure: 返回的数据具有以下结构:

{
"data": {
    "id": 1, 
    "name": "Bitcoin", 
    "symbol": "BTC", 
    "website_slug": "bitcoin", 
    "rank": 1, 
    "circulating_supply": 17008162.0, 
    "total_supply": 17008162.0, 
    "max_supply": 21000000.0, 
    "quotes": {
        "USD": {
            "price": 9024.09, 
            "volume_24h": 8765400000.0, 
            "market_cap": 153483184623.0, 
            "percent_change_1h": -2.31, 
            "percent_change_24h": -4.18, 
            "percent_change_7d": -0.47
        }
    }, 
    "last_updated": 1525137271
},
"metadata": {
    "timestamp": 1525237332, 
    "error": null
}

} }

Also, I have no errors as I can see the data structure in the console. 另外,我没有错误,因为可以在控制台中看到数据结构。

-----EDIT---- - - -编辑 - -

I left out a major detail. 我省略了一个主要细节。 I am actually using React 360 for creating a Vr web page. 我实际上是使用React 360创建一个Vr网页。 Sorry for leaving that out. 抱歉,把它遗漏了。

I have tried the same way but not getting any error, its working as expected. 我已经尝试了相同的方法,但是没有收到任何错误,它的工作符合预期。

Only thing i have changed is am using <div> insted <View> , <h1> instead of <Text> . 我唯一改变的是使用<div>插入<View><h1>而不是<Text> since its a react app. 由于它是一个反应应用程序。

You have to check weather are you importing this file correctly or not. 您必须检查天气是否正确导入此文件。

 class CryptoInformation extends React.Component { state = { cryptoInformation: {} } componentDidMount() { fetch('https://api.coinmarketcap.com/v2/ticker/1/') .then(response => response.json()) .then(response => { this.setState({cryptoInformation: response["data"]}); }) .then(response => { console.log(this.state.cryptoInformation.name); }) } render() { return ( <div> <h1>{this.state.cryptoInformation.name}</h1> </div> ) } } ReactDOM.render( <CryptoInformation/>, document.getElementById("root") ); 
 <script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react.min.js"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react-dom.min.js"></script> <div id="root"></div> 

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

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