리액트를 슬기롭게 다루기 위해 "리액트를 다루는 기술" 읽기를 시작했습니다.
- 책을 읽으면서 중요한 내용과 나중에 개발을 하며 참고할 만한 내용을 정리합니다.
- 빠르게 읽기보다는 각 내용을 하나하나 이해하고 적용하는데 집중합니다.
![]()
장점
단점
→ 하지만 리액트의 hooks이 도입되면서 해당 문제는 해결되었다.
const MyComponent = (props) => {
return <div>my name is {props.name}.</div>;
};
MyComponent.defaultProps = {
name : "jake"
};
const App = () => {
return <MyComponent>자식</MyComponent>;
};
const MyComponent = (props) => {
return <div>my name is {props.childern}.</div>;
};
// --> props.childern === 자식
import PropTypes from 'prop-types';
MyComponent.propTypes = {
name : PropTypes.string;
};
MyComponent.propTypes = {
name : PropTypes.string.isRequired;
};
state
props
const array = [1, 2];
const one = array[0] // 1;
const two = array[1] // 2;
const [one, two] = array;
// 객체 다루기
const object = { a: 1, b: 2, c: 3 };
// 사본을 만들어 b값만 덮어쓰기
const nextObject = { ...object, b: 5 };
// 배열 다루기
const arr = [
{ id: 1, value: true },
{ id: 2, value: true },
{ id: 3, value: false },
];
// 새 항목 추가
let nextArr = arr.concat({ id: 4 });
// id가 1인 항목의 value를 false로 설정
nextArr.map((item) => (item.id === 1 ? { ...item, value: false } : item));
// 객체 다루기
const object = { a: 1, b: 2, c: 3 };
// 사본을 만들어 b값만 덮어쓰기
const nextObject = { ...object, b: 5 };
// 배열 다루기
const arr = [
{ id: 1, value: true },
{ id: 2, value: true },
{ id: 3, value: false },
];
// 새 항목 추가
let nextArr = arr.concat({ id: 4 });
// id가 1인 항목의 value를 false로 설정
nextArr.map((item) => (item.id === 1 ? { ...item, value: false } : item));