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

refactor(ui/system): createContext 함수의 인자를 간략화, 반환값 Provider, useContext이 포함된 배열로 변경 #322

Merged
merged 2 commits into from
Aug 21, 2024

Conversation

sukvvon
Copy link
Member

@sukvvon sukvvon commented Aug 21, 2024

이슈 번호

작업한 목록을 작성해 주세요

  • 이슈 내용과 같습니다.

스크린샷

pr 포인트나 궁금한 점을 작성해 주세요

Copy link

vercel bot commented Aug 21, 2024

The latest updates on your projects. Learn more about Vercel for Git ↗︎

Name Status Preview Comments Updated (UTC)
favolink-web ✅ Ready (Inspect) Visit Preview 💬 Add feedback Aug 21, 2024 1:10pm

@github-actions github-actions bot added the refactor 코드 리팩토링, 구조 개선, 성능 향상 등 기능 변경 없이 코드 자체를 개선하는 작업 label Aug 21, 2024
Comment on lines -8 to +12
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);
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

기존 name, hookName이 포함된 객체 인자를 제거하고 rootComponentName, defaultValue로 인자를 간략화합니다.

Comment on lines +17 to +18
// eslint-disable-next-line react-hooks/exhaustive-deps
const value = useMemo(() => context, Object.values(context)) as T;
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

useMemo를 사용하여 context의 값들이 변경되었을 때만 재생성하게 하여 재렌더링을 방지합니다.

Comment on lines +14 to +23
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';
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Provider를 컴포넌트처럼 사용할 수 있게 변경합니다.

@sukvvon sukvvon merged commit 87d75aa into main Aug 21, 2024
5 checks passed
@sukvvon sukvvon deleted the refactor/318 branch August 21, 2024 13:23
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
refactor 코드 리팩토링, 구조 개선, 성능 향상 등 기능 변경 없이 코드 자체를 개선하는 작업
Projects
None yet
1 participant