- react selector
- key : selector를 구분할 수 있는 유일한 id, 즉 key 값을 의미합니다.
- get : 에는 derived state 를 return 하는 곳 입니다. 예시 코드에서는 api call을 통해 받아온 data를 return 하게 됩니다. (해당 selector가 갖고 있습니다.)
```
const cookie = useRecoilValue(selector)
```
다음과 같이 값을 조회할 수 있습니다.
- set : writeable 한 state 값을 변경할 수 있는 함수를 return 하는 곳 입니다. 여기서 주의하실 점은, 자기 자신 selector를 set 하려고 하면, 스스로를 해당 set function에서 set 하는 것이므로 무한루프가 돌게 되니 반드시 다른 selector와 atom을 set 하는 로직을 구성하여야 합니다. 또한 애초에 selector는 read-only 한 return 값(RecoilValue)만 가지기 때문에 set으로는 writeable 한 atom 의 RecoilState 만 설정할 수 있습니다.
```
set: ({set}, newValue) =>{ set(getCookieSelector, newValue) } // incorrect : cannot allign itself
set: ({set}, newValue) =>{ set(cookieState, newValue) } // correct : can allign another upstream atom that is writeable RecoilState
const [cookie, setCookie] = useRecoilState(cookieState);
```
'공부기록' 카테고리의 다른 글
공부기록, 2021-08-30(react 버튼) (0) | 2021.08.30 |
---|---|
공부기록, 2021-08-27(react atom) (0) | 2021.08.27 |
공부기록, 2021-08-25(recoil useRecoilState) (0) | 2021.08.25 |
공부기록, 2021-08-24(recoil) (0) | 2021.08.24 |
공부기록, 2021-08-23(react) (0) | 2021.08.23 |