How to display json data with ReactJs as table

后端 未结 2 927
别那么骄傲
别那么骄傲 2020-12-05 12:18

I would like to display my json data with reactJs in the table, but I can\'t.

[
  {
    \"userId\": 1,
    \"id\": 1,
    \"title\": \"sunt aut facere repell         


        
2条回答
  •  温柔的废话
    2020-12-05 13:02

    You can use map to iterate over your JSON data

    class App extends React.Component {
      constructor(){
        super() 
          this.state = {
            data: []
          }
        
      }
      componentDidMount() {
        $.ajax({
           url: "http://jsonplaceholder.typicode.com/posts",
           type: "GET",
           dataType: 'json',
           ContentType: 'application/json',
           success: function(data) {
             
             this.setState({data: data});
           }.bind(this),
           error: function(jqXHR) {
             console.log(jqXHR);
           }.bind(this)
        })
      }
      render() {
        
            
        return (
          {this.state.data.map(function(item, key) {
                 
                   return (
                      
                    )
                 
                 })}
    {item.userId} {item.id} {item.title} {item.body}
    ) } } ReactDOM.render(, document.getElementById('app'))
    
    
    
    

提交回复
热议问题