Replies: 1 comment
-
// 상태의 정의 (초기값, action 메서드 map)
const countVar = createVar(0, {
increment:(prev) => prev + 1,
decrement:(prev) => prev -1,
reset: () => 0,
})
// 상태 사용 (컴포넌트 내)
const count = useReactiveVar(countVar)
// 업데이트
countVar(1)
// 최신값 가져오기 like ref
countVar() // returns true
// action 사용
countVar.increment()
// 고민 포인트
postfix 를 Var 말고 다른걸루.
// 왜 좋았는지
// useEffect 를 쓰다보면 상태의 최신값을 참조하고 싶은데,
// 그러려면 deps에 넣어줘야하고. 이것이 의도하지 않게 훅을 계속 호출하게 됨.
// (즉 참조는 최신으로 하되, 트리거에는 추가하고 싶지 않을 때가 많음)
// e.g.
useEffect( () => {
}, [count])
ref 처럼 최신값을 가져오는 단일 참조를 유지한다면
다음 처럼 사용가능
useEffect( () => {
const count = countVar() // 항상 최신값
}, []) |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
😉
Beta Was this translation helpful? Give feedback.
All reactions