๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
React ์‹œ๋ฆฌ์ฆˆ๐ŸŒ/ReactJS๋กœ ์˜ํ™” ์›น ์„œ๋น„์Šค ๋งŒ๋“ค๊ธฐ

๐Ÿฟ์˜ค๋Š˜ ๊ณต๋ถ€ํ•œ ๊ฒƒ (์ •๋ฆฌ x)๐Ÿฟ

by @ENFJ 2021. 8. 20.
์ •๋ฆฌํ•˜๋ฉด์„œ ๊ธ€ ์ž‘์„ฑ์‹œ ์‹œ๊ฐ„์ด ๋„ˆ๋ฌด ๊ฑธ๋ฆด๊ฑฐ ๊ฐ™์•„์„œ ๊ทธ๋ƒฅ ์ฝ”๋“œ ๋ž‘ ์ถœ๋ ฅ๊ฒฐ๊ณผ๋ฅผ ์œ„์ฃผ๋กœ ์˜ฌ๋ฆฝ๋‹ˆ๋‹ค.
- ๊ณต๋ถ€์˜ ๋ชฉ์ ์€ ๊ธ€ ์ž‘์„ฑ์ด ์•„๋‹Œ ์ดํ•ด๊ฐ€ ์šฐ์„ !

 

 

1. ์–ด๋–ป๊ฒŒ object list(๊ฐ์ฒด ๋ฆฌ์ŠคํŠธ)๋ฅผ ๊ฐ€์ ธ์˜ค๋Š”์ง€! ์˜ค์ง JS ์˜ object(๊ฐ์ฒด) ๋“ค๋งŒ

2. map ์ด ํ•˜๋Š”๊ฒƒ์€ rendering ํ•˜์ง€๋งŒ array ๋กœ๋ถ€ํ„ฐ ์šฐ๋ฆฌ์—๊ฒŒ array๋ฅผ ์ค€๋‹ค.

3. map ์€ array ์˜ ๊ฐ item์—์„œ function์„ ์‹คํ–‰ํ•˜๋Š” array๋ฅผ ๊ฐ€์ง€๋Š” javaScript fuction ์ด๋‹ค.

๊ทธ function์˜ result ๋ฅผ ๊ฐ–๋Š” array๋ฅผ ๋„ˆ์—๊ฒŒ ์ค˜.'

 

 

map ์€ ๊ฐ๊ฐ item ๋ณ„๋กœ function ์„ ํ˜ธ์ถœํ•จ.

item ์ด๋ฆ„: dish <- ๋ผ๊ณ   ์ •ํ•ด์คŒ

map ์€ ๋ญ๊ฐ€ ๋Œ์•„์˜ค๋“  array๋กœ ๋Œ๋ ค์ฃผ๊ธฐ ๋•Œ๋ฌธ

 

import React from 'react';  // ๋ฆฌ์—‘ํŠธ๋ฅผ ์‚ฌ์šฉํ•˜๋ ค๋ฉด ํ•„์ˆ˜ ๊ตฌ๋ฌธ.
import PropTypes from "prop-types";


const foodILike = [
  {
    id:1,
    name:"kimchi",
    image:
    "http://m.8go.kr/web/product/big/202102/a7aa14360fdb47b585ae860555369f30.jpg",
    rating:3.0
  },
  {
    id:2,
    name:"samgyeopsal",
    image:
    " http://image.newsis.com/2020/06/17/NISI20200617_0000546793_web.jpg",
    rating:5.0
  },
  {
    id:3,
    name:" bibimbap",
    image:
    "https://upload.wikimedia.org/wikipedia/commons/thumb/4/44/Dolsot-bibimbap.jpg/220px-Dolsot-bibimbap.jpg",
    rating:4.0
  },
  {
    id:4,
    name:"jukumi",
    image:
    " https://recipe1.ezmember.co.kr/cache/recipe/2019/03/05/52d2be99c015378a75c9db81362422c71.jpg",
    rating:1.0
  }
];
  //ํ•จ์ˆ˜ Food component
function Food({name, picture, rating}){   //props.fav ๋˜๋Š” {}๋‚ด๋ถ€์— fav๋ฅผ ์“ฐ๋Š”๊ฒƒ์€ ๊ฐ™์€๊ฑฐ์ž„.
  return( 
    <div>
      <h2>i like {name}</h2>
      <h4>{rating}/5.0</h4>
      <img src={picture} alt={name} />
    </div>
  );
}

Food.propTypes = {
  name: PropTypes.string.isRequired,
  picture: PropTypes.string.isRequired,
  rating : PropTypes.number.isRequired
}

function App() {                    //์ด ์• ํ”Œ๋ž˜์ผ€์ด์…˜์— food ์ปดํฌ๋„ŒํŠธ ์ •๋ณด๋ฅผ ๋ณด๋‚ด๊ณ , ๊ทธ๋Ÿฐ๋‹ค์Œ์— food ์ปดํฌ๋„ŒํŠธ์—์„œ ๊ทธ์ •๋ณด๋ฅผ ์–ด๋–ป๊ฒŒ ์‚ฌ์šฉํ•˜๋Š”์ง€!
  return (
     <div> 
        {foodILike.map(dish => (
          <Food key={dish.id} name={dish.name} picture={dish.image} rating={dish.rating} />//props
          ))}
     </div> 
    );
}

export default App;
//food component ์— ์ •๋ณด๋ฅผ ๋ณด๋‚ด๋Š” ๋ฐฉ๋ฒ•.  -> food component์— fav๋ผ๋Š” ์ด๋ฆ„์˜ propery๋ฅผ kimchi๋ผ๋Š” value๋กœ ์ค€๊ฑฐ์ž„.

 

 

 

 

import React from 'react';  // ๋ฆฌ์—‘ํŠธ๋ฅผ ์‚ฌ์šฉํ•˜๋ ค๋ฉด ํ•„์ˆ˜ ๊ตฌ๋ฌธ.
import PropTypes from "prop-types";

class App extends React.Component {
  state={
    count: 0  
  }
  add= () =>{
    this.setState(current =>({count: current.count +1}))
  };
  minus= () =>{
    this.setState(current =>({count: current.count -1}))
  };
  render(){
    return(
    <div>
      <h1>The number is : {this.state.count}</h1>
      <button onClick={this.add}>Add</button>
      <button onClick={this.minus}>Minus</button>
    </div>
    );
  }
} 
                             //class App ์€ react component . ์ด๋‹จ๊ณ„๋Š” ํ•„์ˆ˜
export default App;

 

 

app.js

import React from 'react';  // ๋ฆฌ์—‘ํŠธ๋ฅผ ์‚ฌ์šฉํ•˜๋ ค๋ฉด ํ•„์ˆ˜ ๊ตฌ๋ฌธ.
import axios from 'axios';
import Movie from "./movies";

class App extends React.Component {
  state ={
    isLoading: true,
    movies:[ ]
  };
  getMovies = async() => {
    const {data: {data:{movies}}} = await axios.get("https://yts-proxy.nomadcoders1.now.sh/list_movies.json?sort_by=rating")
   this.setState({movies, isLoading:false})
  }

  componentDidMount(){
    this.getMovies();
  }

  render(){
    const { isLoading, movies } = this.state;
    return (
     
      <section class="container">
        {isLoading ? (
          <div class="loader">
            <span class="loader__text">Loading...</span>
          </div>
      ) : (
      <div class="movies">
        {movies.map(movie => (
            <Movie
              key={movie.id}
              id={movie.id}
              year={movie.year}
              title={movie.title} 
              summary={movie.summary} 
              poster={movie.medium_cover_image}
              />
            ))}
      </div>
          
        )}
      </section>
    );
  }
}

export default App;

movies.js

 

import React from "react";
import PropTypes from "prop-types";


function Movie({id,year,title,summary,poster}){
  return (
  <div class="movies_movie">
    <h3 class="movie_title">{title}</h3>
    <h5 class="movie_year">{year}</h5>
    <p class="movie_summary">{summary}</p>
    </div>
    );
}

Movie.propTypes= {
  id: PropTypes.number.isRequired,
  year: PropTypes.number.isRequired,
  title: PropTypes.string.isRequired,
  summary: PropTypes.string.isRequired,
  poster: PropTypes.string.isRequired
}

export default Movie;

index.js

import React from 'react';
import ReactDOM from 'react-dom';

import App from './App';



ReactDOM.render(<App/>, document.getElementById('root'));