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

buildContext 함수 수정 (issue #22) #23

Merged
merged 1 commit into from
Oct 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 5 additions & 8 deletions packages/react/react/src/utils/buildContext.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,13 @@ Context를 정의할때 발생하는 보일러플레이트를 줄여주는 함

```tsx
import { buildContext } from './buildContext';
import React from 'react';

interface CountContextProps {
count: number;
setCount: (newCount: number) => void;
}

const [CountProvider, useCount] = buildContext<CountContextProps>('Count', {
const [CountProvider, useCountContext] = buildContext<CountContextProps>('Count', {
count: 0,
setCount: () => {},
});

export default function App() {
Expand All @@ -33,16 +30,16 @@ export default function App() {
}

const Inner = () => {
const { count, setCount } = useCount();
const { count, updateContext } = useCountContext();

const incrementCount = () => {
setCount(count + 1);
const handleIncrementCount = () => {
updateContext({ count: count + 1 });
};

return (
<>
<div>Count: {count}</div>
<button onClick={incrementCount}>Increment</button>
<button onClick={handleIncrementCount}>Increment</button>
</>
);
};
Expand Down
31 changes: 16 additions & 15 deletions packages/react/react/src/utils/buildContext.tsx
Original file line number Diff line number Diff line change
@@ -1,39 +1,40 @@
// buildContext.tsx
import { createContext, useContext, useState, useMemo, ReactNode } from 'react';

type ProviderProps<T> = {
children: ReactNode;
value?: Partial<T>;
};

export function buildContext<T extends object>(contextName: string, defaultContext: T) {
type ContextWithUpdate = T & { updateContext: (updates: Partial<T>) => void };

const Context = createContext<ContextWithUpdate | null>(null);
export function buildContext<T>(contextName: string, defaultContext: T) {
const Context = createContext<(T & { updateContext: (updates: Partial<T>) => void }) | null>(
null,
);

function Provider({ children, value }: ProviderProps<T>) {
const [state, setState] = useState<T>(() => ({
...defaultContext,
...value,
}));

const contextValue = useMemo(() => {
const updateContext = (updates: Partial<T>) => {
setState((prevState) => ({ ...prevState, ...updates }));
};
const updateContext = (updates: Partial<T>) => {
setState((prevState) => ({
...prevState,
...updates,
}));
};

return {
...state,
updateContext,
};
}, [state]);
const contextValue = useMemo(() => ({ ...state, updateContext }), [state]);

return <Context.Provider value={contextValue}>{children}</Context.Provider>;
}

function useContextHook(): ContextWithUpdate {
function useContextHook(): T & {
updateContext: (updates: Partial<T>) => void;
} {
const context = useContext(Context);

if (context === null || context === undefined) {
if (!context) {
throw new Error(`use${contextName} must be used within a ${contextName}Provider`);
}

Expand Down