Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

React Hook 간단 #31

Open
gincheong opened this issue Feb 17, 2021 · 3 comments
Open

React Hook 간단 #31

gincheong opened this issue Feb 17, 2021 · 3 comments

Comments

@gincheong
Copy link
Owner

No description provided.

@gincheong
Copy link
Owner Author

useEffect에서 Clean-up 함수 사용하기

useEffect(() => {
  ...  
  return () => {
    // 여기의 코드는, 라이프사이클이 돌아가면서 useEffect가 새로 실행되기 이전에 실행됨
  };
});

그러니까 state가 새로 바뀔 때, 직전의 state에 대한 처리를 저기서 할 수 있음

@gincheong
Copy link
Owner Author

gincheong commented Feb 17, 2021

useRef로 이전 state의 값 기억하기

const prevValueRef = useRef();
const prevValue = prevValueRef.current;

useEffect(() => {
  console.log('value updated:', prevValue, '->', value);
});
const inProgressTasks = List(store.get('task_list').filter(each => !each.completed));

const prevInProgressTasks = useRef(List());

useEffect(() => {
  prevInProgressTasks.current = inProgressTasks;
}, [inProgressTasks]);

@gincheong
Copy link
Owner Author

gincheong commented Feb 18, 2021

useEffect(() => {
  console.log('value updated:', prevValue, '->', value);
}, []); // 이 코드는 한 번만 실행됨

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant