API 본죽 사용해보기

Durumi Gim·2021년 3월 11일
0

ProductList.js

import React, { Component } from "react";
import Product from "./Product";

class ProductList extends Component {
  state = {
    product: [],
  };

  componentDidMount() {
    fetch("http://localhost:3000/data/ProductListData.json")
      .then((res) => res.json())
      .then((res) => {
        this.setState({
          product: res,
        });
      });
  }

  render() {
    console.log("render");
    return (
      <div>
        {this.state.product.map((el) => {
          return <Product name={el.name} img={el.img} cost={el.cost} />;
        })}
      </div>
    );
  }
}

export default ProductList;

ProductList.js

import React, { Component } from "react";

class Product extends Component {
  render() {
    console.log(this.props.product);
    return (
      <div>
        <img src={this.props.img} />
        <p>{this.props.name}</p>
        <p>{this.props.cost}</p>
      </div>
    );
  }
}

export default Product;
profile
마음도 몸도 튼튼한 개발자

0개의 댓글