from here: http://stackoverflow.com/questions/40347411/how-to-display-json-data-with-reactjs-in-the-table
Created
January 1, 2023 00:22
-
-
Save dirtydanisreal/7862995e9cd306ac6cd1edda9efbf91c to your computer and use it in GitHub Desktop.
React JSON Table Example
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<div id="app"></div> | |
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script> | |
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/0.14.8/react.js"></script> | |
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/0.14.8/react-dom.js"></script> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 ( | |
<table> | |
<tbody>{this.state.data.map(function(item, key) { | |
return ( | |
<tr key = {key}> | |
<td>{item.userId}</td> | |
<td>{item.id}</td> | |
<td>{item.title}</td> | |
<td>{item.body}</td> | |
</tr> | |
) | |
})}</tbody> | |
</table> | |
) | |
} | |
} | |
ReactDOM.render(<App/>, document.getElementById('app')) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment