javascript - How to render data from API response in React? - Stack Overflow

how to initialize response from API to variable so I can use it in my render() function? Why ponent.set

how to initialize response from API to variable so I can use it in my render() function? Why ponent.setState is not working? Error:
Uncaught TypeError: Cannot read property 'username' of undefined(…)

class Main extends React.Component {
 constructor(props) {
     super(props);
    this.state = {
      data: 23
    };
 }

  ponentDidMount() {
var ponent = this;


      fetch('')
          .then(function(response) {
              return response.json()
          }).then(function(json) {
              var data = json;
              console.log(data[0]);
              console.log(data[0].username);
              ponent.setState({
                  data: json
              })
          })

  }




  render() {
    return (
      <div>
        <h1>elo{this.state.data[0].username}</h1>


      </div>
    );
  }
}

const docs = document.getElementById('root');

ReactDOM.render( <Main/> , docs);

how to initialize response from API to variable so I can use it in my render() function? Why ponent.setState is not working? Error:
Uncaught TypeError: Cannot read property 'username' of undefined(…)

class Main extends React.Component {
 constructor(props) {
     super(props);
    this.state = {
      data: 23
    };
 }

  ponentDidMount() {
var ponent = this;


      fetch('https://fcctop100.herokuapp./api/fccusers/top/alltime')
          .then(function(response) {
              return response.json()
          }).then(function(json) {
              var data = json;
              console.log(data[0]);
              console.log(data[0].username);
              ponent.setState({
                  data: json
              })
          })

  }




  render() {
    return (
      <div>
        <h1>elo{this.state.data[0].username}</h1>


      </div>
    );
  }
}

const docs = document.getElementById('root');

ReactDOM.render( <Main/> , docs);
Share Improve this question edited Mar 17, 2022 at 9:42 VLAZ 29.1k9 gold badges63 silver badges84 bronze badges asked Nov 14, 2016 at 16:59 KATKAT 531 gold badge2 silver badges8 bronze badges 2
  • You should have a guard statement / ternary operator in your render method if you plan on using some data that will arrive at a later time. Something like this: var someData = this.state.data[0].username || ""; – Justin Herter Commented Nov 14, 2016 at 17:07
  • @JustinHerter console.logs in fetch logs proper data – KAT Commented Nov 14, 2016 at 17:13
Add a ment  | 

1 Answer 1

Reset to default 6

The reason is that on first render this "this.state.data[0].username" is not set, until the response es back from the API call.

Something like this in your render method should work:

render() {
    var someData = this.state.data[0].username || "";
    return (
      <div>
        <h1>elo{someData}</h1>


      </div>
    );
  }

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1742284541a4415090.html

相关推荐

  • javascript - How to render data from API response in React? - Stack Overflow

    how to initialize response from API to variable so I can use it in my render() function? Why ponent.set

    22小时前
    30

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信