Skip to content

Commit

Permalink
Merge pull request #21 from brgndyy/feat/#20
Browse files Browse the repository at this point in the history
buildContext 함수 수정 (feat/#20)
  • Loading branch information
brgndyy authored Oct 12, 2024
2 parents 0ccc7d9 + a152a7e commit 50cf7e2
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 5 deletions.
2 changes: 1 addition & 1 deletion packages/react/react/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@brgndy/react",
"version": "1.0.3",
"version": "1.0.4",
"sideEffects": false,
"main": "dist/index.js",
"module": "dist/index.esm.js",
Expand Down
10 changes: 6 additions & 4 deletions packages/react/react/src/utils/buildContext.tsx
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
import React, { createContext, useContext, useState, useMemo, ReactNode } from 'react';
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) {
const Context = createContext<T | null>(null);
type ContextWithUpdate = T & { updateContext: (updates: Partial<T>) => void };

const Context = createContext<ContextWithUpdate | null>(null);

function Provider({ children, value }: ProviderProps<T>) {
const [state, setState] = useState<T>(() => ({
Expand All @@ -28,10 +30,10 @@ export function buildContext<T extends object>(contextName: string, defaultConte
return <Context.Provider value={contextValue}>{children}</Context.Provider>;
}

function useContextHook(): T {
function useContextHook(): ContextWithUpdate {
const context = useContext(Context);

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

Expand Down

0 comments on commit 50cf7e2

Please sign in to comment.