-
Notifications
You must be signed in to change notification settings - Fork 0
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
refactor(ui/system): createContext 함수의 인자를 간략화, 반환값 Provider, useContext이 포함된 배열로 변경 #322
Conversation
The latest updates on your projects. Learn more about Vercel for Git ↗︎
|
type CreatContextOptions<T> = { | ||
name?: string; | ||
hookName?: string; | ||
providerName?: string; | ||
errorMessage?: string; | ||
defaultValue?: T; | ||
}; | ||
|
||
type CreateContextReturn<T> = [Provider<T>, () => T, Context<T>]; | ||
export function createContext<T extends object | null>( | ||
rootComponentName: string, | ||
defaultValue?: T, | ||
) { | ||
const Context = createReactContext<T | undefined>(defaultValue); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
기존 name, hookName이 포함된 객체 인자를 제거하고 rootComponentName, defaultValue로 인자를 간략화합니다.
// eslint-disable-next-line react-hooks/exhaustive-deps | ||
const value = useMemo(() => context, Object.values(context)) as T; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
useMemo를 사용하여 context의 값들이 변경되었을 때만 재생성하게 하여 재렌더링을 방지합니다.
function Provider(props: T & { children: ReactNode }) { | ||
const { children, ...context } = props; | ||
|
||
export function createContext<T>(options: CreatContextOptions<T>) { | ||
const { | ||
name, | ||
hookName = 'useContext', | ||
providerName = 'Provider', | ||
errorMessage, | ||
defaultValue, | ||
} = options; | ||
// eslint-disable-next-line react-hooks/exhaustive-deps | ||
const value = useMemo(() => context, Object.values(context)) as T; | ||
|
||
const Context = createReactContext<T | undefined>(defaultValue); | ||
return <Context.Provider value={value}>{children}</Context.Provider>; | ||
} | ||
|
||
Context.displayName = name; | ||
Provider.displayName = rootComponentName + 'Provider'; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Provider를 컴포넌트처럼 사용할 수 있게 변경합니다.
이슈 번호
작업한 목록을 작성해 주세요
스크린샷
pr 포인트나 궁금한 점을 작성해 주세요