Answer the question
In order to leave comments, you need to log in
How to render from Json to HTML in React?
Please tell me with a simple example how to display a block with data from it in html from external json.
Conventionally, there is json at the link https://site.com/data.json and the content in it:
[ {
"id": 1569,
"link": "https://site.com"
} ]
<div>
<span>Здесь ID</span>
<span>Здесь значение link</span>
</div>
Answer the question
In order to leave comments, you need to log in
...
state = {
json: [],
}
...
componentDidMount() {
fetch('https://site.com/data.json')
.then(response => response.json())
.then(json => this.setState({ json }));
}
...
render() {
const { json } = this.state;
return (
{json.map(({ id, link })) => (
<div>
<span>{id}</span>
<span>{link}</span>
</div>
)}
)
}
...
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question