[새싹(SeSAC) 프론트엔드] day30 221125
📍defaultProps
: props 를 지정하지 않았을 때 기본적으로 사용할 값을 설정
app.jsx
function App() {
return (
<div className="App">
<DefaultProps name="yuulya" />
<DefaultProps />
</div>
);
}
DefaultProps.jsx
import React from "react";
const DefaultProps = (props) => {
const { name } = props;
return <div>안녕하세요 제 이름은 {name}입니다</div>;
};
// ** 컴포넌트이름.defaultProps
DefaultProps.defaultProps = {
name: "이름없음",
};
export default DefaultProps;
📍props.children
: 컴포넌트 태그 사이에 넣은 값을 조회할 때 사용
Wrapper.jsx
import React from "react";
const Wrapper = ({children}) => { //객체가 children 인 경우만 정상적으로 실행됨
const style = {
border: "3px solid red",
padding: "20px",
};
return <div style={style}>
{children}
</div>;
};
export default Wrapper;
app.js
function App() {
return (
<div className="App">
{/* <DefaultProps name="yuulya" />
<DefaultProps /> */}
<Wrapper>
<CreateHeader />
<CreateBody />
<CreateFooter />
</Wrapper>
</div>
);
}
** children은 예약어라서 이러한 형식으로 사용하고 싶다면 children을 써줘야 된다!
propTypes
: 특정 컴포넌트에 꼭 전달되어야 하는 프로퍼티를 필수 프로퍼티로 지정하거나 프로퍼티의 타입을 지정할 때 사용
import PropTypes from 'prop-types';
proptypes 와 app.js에서의 타입이 다르면 에러가 나지만 값은 정상적으로 출력됨
PropTypesComponent.jsx
import React from "react";
import PropTypes from "prop-types";
const PropTypesComponent = ({ name }) => {
return <div>안녕하세여 제 이름은 {name} 입니다</div>;
};
PropTypesComponent.propTypes = {
name: PropTypes.string,
favoriteNumber: PropTypes.number.isRequired,
};
export default PropTypesComponent;
📍배열 컴포넌트
- 출력해야 하는 정보가 너무 많은 경우
- 서버에서 데이터를 받아오는 경우 몇개의 데이터가 넘어올지 알 수 없는 경우
➡️ map() 함수로 해결
map()함수
: 배열 안의 모든 요소들의 값을 변경해서 새로운 배열을 생성하는 함수
function App() {
const movieList = [
{
title: "해리포터",
open: "2002.10.22",
poster:
"url1",
},
{
title: "반지의 제왕",
open: "2002.10.22",
poster:
"url2",
},
{
title: "스타워즈",
open: "2002.10.22",
poster:
"url3",
},
];
return (
<>
{movieList.map((movie) => (
<MovieComponent
title={movie.title}
open={movie.open}
poster={movie.poster}
/>
))}
</>
);
}
movieComponent.jsx
import React from "react";
const MovieComponent = (props) => {
const { title, open, poster } = props;
return (
<div>
영화제목 : {title}
<br></br>
개봉일 : {open}
<br></br>
<img src={poster}></img>
<br></br>
<hr />
</div>
);
};
export default MovieComponent;
**img에 alt를 안 넣어주면 오류가 뜸
📍key props
: 리액트의 원소가 리스트에 포함되면서 리액트 원소들의 유일성이 사라지게 되는데 이를 배열 속성에 key값을 추가해주는 것을 통해 해결 가능함
: 배열 컴포넌트는 배열 원소의 개수만큼 반복하므로 성능에 영향을 많이 줌
- 배열에 key값을 추가하면 성능향상에 도움이 됨
: key 값을 정의하여 출력한 배열 컴포넌트를 다시 출력해야 하는 경우, 리엑트 엔진이 기존 컴포넌트를 재활용하여 성능을 높일 수 있음
- key props는 리액트 내부에서 사용되는 특수한 props이 기 때문에 하위 컴포넌트에는 전달되지 않음
📍React ui framework
https://aglowiditsolutions.com/blog/best-react-ui-framework/#MaterialKitReact
Best React UI Framework You Should Know In 2022
In Search of the best React UI framework - Get an idea of the top Component Framework for React, their features & popularity status.
aglowiditsolutions.com
MUI: The React component library you always wanted
MUI provides a simple, customizable, and accessible library of React components. Follow your own design system, or start with Material Design.
mui.com
새싹DT 기업연계형 프론트엔드 실무 프로젝트 과정 6주차 블로그 포스팅(금)