js primitive

1
2
const PrimitiveType =
string | number | bigint | boolean | undefined | symbol | null;
  • primitive value are immutable

array fill

Array.fill
1
2
3
4
5
6
7
const arr1 = new Array(2).fill({});
arr1[0].a = 1;
console.log(arr1); // [{a:1}, {a:1}] // 전부다 적용됨

const arr2 = new Array(2).fill(0);
arr2[0] = 1;
console.log(arr2); // [1, 0] // 하나만 적용됨

react state

useState
1
2
3
4
5
6
7
8
9
10
11
12
13
14
const [state, setState] = useState(0);
const [state2, setstate2] = useState<number[]>([]);

const onClickHandler = () => {
setState(1); // 제대로 렌더링 됨

state2.push(1);
setstate2(state2); // 레퍼런스가 달라지지 않아서 리렌더링 안됨
console.log({ state2 }); // 로그는 제대로 찍힘
};

const onClickHandlerFix = () => {
setstate2((prev) => [...prev, 1]); // 레퍼런스를 바꿔줘야 리렌더링
};

참고

Author

chinsung

Posted on

2021-11-13

Updated on

2021-11-13

Licensed under

댓글