์ ๋ฆฌํ๋ฉด์ ๊ธ ์์ฑ์ ์๊ฐ์ด ๋๋ฌด ๊ฑธ๋ฆด๊ฑฐ ๊ฐ์์ ๊ทธ๋ฅ ์ฝ๋ ๋ ์ถ๋ ฅ๊ฒฐ๊ณผ๋ฅผ ์์ฃผ๋ก ์ฌ๋ฆฝ๋๋ค.
- ๊ณต๋ถ์ ๋ชฉ์ ์ ๊ธ ์์ฑ์ด ์๋ ์ดํด๊ฐ ์ฐ์ !
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'));