V
V
Vlad Ermolov2019-12-23 01:13:39
JavaScript
Vlad Ermolov, 2019-12-23 01:13:39

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"
    } ]

How to output to React this in
<div>
<span>Здесь ID</span>
<span>Здесь значение link</span>
</div>

Answer the question

In order to leave comments, you need to log in

2 answer(s)
L
lnked, 2019-12-23
@Ermolov

...
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>
        )}
    )
}
...

M
McBernar, 2019-12-23
@McBernar

Import the json Access
it via map
Render all the elements of the array Where
do you have a problem?

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question