React
[React] Protection with PropTypes
꿈나무 김땡땡
2020. 7. 27. 08:57
- foodILike list에 rating 추가
- terminal > npm i prop-types (설치)
- package.json > dependencies > prop-types가 있으면 설치 완료!
- prop-types : 우리가 원하는 component가 맞는지 확인 시켜준다.
- App.js > import PropTypes from "prop-types";
- App.js > Food.propTypes 정보 추가 (type, required 여부)
참고
https://ko.reactjs.org/docs/typechecking-with-proptypes.html
PropTypes와 함께 하는 타입 확인 – React
A JavaScript library for building user interfaces
ko.reactjs.org
출처
https://nomadcoders.co/react-fundamentals/lectures/1551
All Courses – Nomad Coders
초급부터 고급까지! 니꼬쌤과 함께 풀스택으로 성장하세요!
nomadcoders.co
코드
import React from 'react';
import PropTypes from "prop-types";
function Food({name, picture, rating}) {
return <div>
<h2>I like {name}</h2>
<h4> {rating} / 5.0 </h4>
<img src={picture} alt={name}/>
</div>
}
// propTypes 추가
Food.propTypes = {
name: PropTypes.string.isRequired,
picture: PropTypes.string.isRequired,
rating: PropTypes.number
}
const foodILike =[
{
id: 1,
name: "Kimchi",
image:
"http://aeriskitchen.com/wp-content/uploads/2008/09/kimchi_bokkeumbap_02-.jpg",
rating: 4
},
{
id: 2,
name: "Samgyeopsal",
image:
"https://3.bp.blogspot.com/-hKwIBxIVcQw/WfsewX3fhJI/AAAAAAAAALk/yHxnxFXcfx4ZKSfHS_RQNKjw3bAC03AnACLcBGAs/s400/DSC07624.jpg",
rating: 5
},
{
id: 3,
name: "Bibimbap",
image:
"http://cdn-image.myrecipes.com/sites/default/files/styles/4_3_horizontal_-_1200x900/public/image/recipes/ck/12/03/bibimbop-ck-x.jpg?itok=RoXlp6Xb",
rating: 3.7
},
{
id: 4,
name: "Doncasu",
image:
"https://s3-media3.fl.yelpcdn.com/bphoto/7F9eTTQ_yxaWIRytAu5feA/ls.jpg",
rating: 3.5
},
{
id: 5,
name: "Kimbap",
image:
"http://cdn2.koreanbapsang.com/wp-content/uploads/2012/05/DSC_1238r-e1454170512295.jpg",
rating: 5
}
]
function App() {
return (
<div>
{foodILike.map(dish => (
<Food key={dish.id} name={dish.name} picture={dish.image} rating={dish.rating} />
))}
</div>
);
}
export default App;
https://github.com/grace-kimm/movie_app_2020/commit/7510806a0a4873a928f67671abbd848ec12ab9da
#2.4 Protection with PropTypes · grace-kimm/movie_app_2020@7510806
Permalink Browse files #2.4 Protection with PropTypes Loading branch information Showing 2 changed files with 29 additions and 9 deletions. +1 −0 package.json +28 −9 src/App.js @@ -6,6 +6,7 @@ "@testing-library/jest-dom": "^4.2.4", "@testing-library/re
github.com