react코드가 궁금해서 찾아보다가

https://github.com/facebook/react/blob/41f0e9dae3b81396dc29a4735b355ea318cc5772/packages/react-server/src/ReactFizzHooks.js#L361
여기에 useMemo, useReducer, useState 등등이 있길래 보고잇는데,



1. 

  1. export function useState<S>(
  2. initialState: (() => S) | S,
  3. ): [S, Dispatch<BasicStateAction<S>>] {
  4. if (__DEV__) {
  5. currentHookNameInDev = 'useState';
  6. }
  7. return useReducer(
  8. basicStateReducer,
  9. // useReducer has a special case to support lazy useState initializers
  10. (initialState: any),
  11. );
  12. }

useState는 이런 식으로 구현되어있는데,

이거는 useState가 useReducer에서 특정기능만 사용하도록 감싸서 만든 거임???

2. useReducer has a special case to support lazy useState initializers 1번 질문이랑 관련해서 이거 해석이 궁금한데,

ㄱ. useState가 lazy initializer일때는 useReducer가 갖고 있는 special case로 쓰이로 구현이 된다 뭐 이런걸까??
ㄴ. 아님 걍 1번처럼, useReducer가 special case를 갖고 있는데, 그게 useState이다 

이거일까??




3. useMemo에서

  1. function useMemo<T>(nextCreate: () => T, deps: Array<mixed> | void | null): T {
  2. currentlyRenderingComponent = resolveCurrentlyRenderingComponent();
  3. workInProgressHook = createWorkInProgressHook();
  4.  
  5. const nextDeps = deps === undefined ? null : deps;
  6.  
  7. if (workInProgressHook !== null) {
  8. const prevState = workInProgressHook.memoizedState;
  9. if (prevState !== null) {
  10. if (nextDeps !== null) {
  11. const prevDeps = prevState[1];
  12. if (areHookInputsEqual(nextDeps, prevDeps)) {
  13. return prevState[0];
  14. }
  15. }
  16. }
  17. }
  18.  
  19. if (__DEV__) {
  20. isInHookUserCodeInDev = true;
  21. }
  22. const nextValue = nextCreate();
  23. if (__DEV__) {
  24. isInHookUserCodeInDev = false;
  25. }
  26. // $FlowFixMe[incompatible-use] found when upgrading Flow
  27. workInProgressHook.memoizedState = [nextValue, nextDeps];
  28. return nextValue;
  29. }

memo를 쓸때만 써야된다고 배웠는데.

원본코드 보니깐

피보나치 구현할 때 공부한 memoization이 사용 된 거 같음.
dependency가 같은 지 확인한 다음,

같으면 prevState[0]번 넘겨주고 아니면 
const nextValue = nextCreate();로 하나 만들어서 넘겨주고 얘는 저장하고..
이런 거는 스택(재귀함수에서 함수호출하면 쌓이는 그 스택)에 저장되는 게 아니라 메모리를 써서 저장하는 거니까

걍 어지간하면 이득아님???

메모리 공간을 차지하는 게 단점일 거 같긴한데, 
어차피 리액트에서 내부적으로 뭔가를 만들어가지고 그거를 dom에 뿌리는 거라고 배웠는데,
그 뭔가를 저장해놓고, dependency가 같은지만 확인하는거니까 단점이 거의 없는 거아님???