💡 props와 state 차이?state: 컴포넌트 내부에서 바뀔 수 있는 값
1. 클래스형 컴포넌트의 state
2. 함수형 컴포넌트에서 useState에서 사용하는 state
const array = [1,2];
const [one, two] = array;
const [message, setMEssage] = useState('');
const onClickEnter = () => setMessage('안녕하세요!');
const onClickLeave = () => setMessage('안녕히가세요!');
const []color, setColor] = useState('black');
...
return (
<h1 style={{color}}</h1>
<button style={{color:'red'}} onClick=(()=>setColor('red')}>빨간색</button>
<button style={{color:'green'}} onClick=(()=>setColor('green')}>초록색</button>
<button style={{color:'blue'}} onClick=(()=>setColor('blue')}>파란색</button>
)
const object = {a:1, b:2, c:3};
const nextObject = {...object, b:2}; // 사본을 만들고 b 값만 덮어쓰기
const array = [
{id:1, value: true},
{id:2, value: true},
{id:3, value: false}
];
let nextArray = array.concat({id:4});
nextArray.filter(item => item.id!==2); // id가 2인 항목을 제거
nextArray.map(item => (item.id ===1 ? {...item, value:false} : item));
// id가 1인 항목의 value를 false로 설정
출처: <리액트를 다루는 기술> 3장