X
X
X0lera2018-06-03 09:46:42
React
X0lera, 2018-06-03 09:46:42

How to generate components based on data received via ajax?

import React from 'react'

fetch("http://api.kai-zer.ru/dev/method/products.getAll")
.then((res) => res.json())
.then((data) => {
  const items = data.response.items;
  const Image = (namber) => {
    return (
      <div>
        {items[namber].image.url}
      </div>
      )
    }
})

export default Image

How can I do something like this?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
0
0xD34F, 2018-06-03
@X0lera

const Image = ({ url }) => (
  <div>
    <img src={url} />
  </div>
);

class App extends React.Component {
  state = {
    items: [],
  }

  componentDidMount() {
    fetch('https://api.kai-zer.ru/dev/method/products.getAll')
      .then(r => r.json())
      .then(r => {
        this.setState({
          items: r.response.items,
        });
      });
  }

  render() {
    return (
      <div>
        {this.state.items.map(n => <Image {...n.image} />)}
      </div>
    );
  }
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question