admin管理员组文章数量:1246601
i have JSON like this
i want to use this JSON and display data in Table using react js.
this is how i display data from JSON file.
import React, { Component } from 'react';
import data from './data.json';
class App extends Component {
render() {
return (
<ul>
{
data.map(function(movie){
return <li>{movie.id} - {movie.title}</li>;
})
}
</ul>
);
}
}
export default App;
how to load JSON from URL and display it in table using reactjs?
i have JSON like this
i want to use this JSON and display data in Table using react js.
this is how i display data from JSON file.
import React, { Component } from 'react';
import data from './data.json';
class App extends Component {
render() {
return (
<ul>
{
data.map(function(movie){
return <li>{movie.id} - {movie.title}</li>;
})
}
</ul>
);
}
}
export default App;
how to load JSON from URL and display it in table using reactjs?
Share Improve this question edited Apr 6, 2018 at 15:33 James Z 12.3k10 gold badges27 silver badges47 bronze badges asked Apr 6, 2018 at 10:27 AhmadAhmad 1,4745 gold badges19 silver badges41 bronze badges3 Answers
Reset to default 7You could fetch the JSON once the ponent will mount, and when you eventually resolve it you can update the state of the ponent:
import React, { Component } from 'react';
class App extends Component {
// initially data is empty in state
state = { data: [] };
ponentDidMount() {
// when ponent mounted, start a GET request
// to specified URL
fetch(URL_TO_FETCH)
// when we get a response map the body to json
.then(response => response.json())
// and update the state data to said json
.then(data => this.setState({ data }));
}
render() {
return (
<ul>
{
this.state.data.map(function(movie){
return <li key={movie.id}>{movie.id} - {movie.title}</li>;
})
}
</ul>
);
}
}
export default App;
If you're unable to use fetch
, you could use some other libraries like superagent or axios. Or you could even fall back to good ol' XMLHttpRequest
.
On another note, when building a list of ponent it is important they each child have a unique key
attribute. I also updated that in the code, with the assumption that movie.id
is
Example axios code:
axios.get(URL)
.then(response => response.data)
.then(data => this.setState({ data }));
EDIT: as trixn wrote in a reply, ponentDidMount
is the preferred place to fetch data. Updated code.
EDIT 2: Added axios code.
You can use axios to send http requests.
It looks like this :
const response = await axios.get(your_url_here);
const items = response.data.items;
About await keyword : How to use await key word on react native?
This is axios GitHub page for the docs : https://github./axios/axios
Hope it helps.
You can use the fixed Data table to display the data from the json Response.Since the data is vast and it would be difficult to manage the conventional table, this would be a better alternative.
The documentation is given at
https://github./schrodinger/fixed-data-table-2/blob/master/examples/ObjectDataExample.js
This link will help you.
本文标签: javascripthow to display data in table using json in react jsStack Overflow
版权声明:本文标题:javascript - how to display data in table using json in react js? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1740254835a2249169.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论