Refactor BudgetContext file
Refactor BudgetContext.tsx into smaller components and hooks to improve code readability and maintainability.
This commit is contained in:
27
src/contexts/budget/BudgetContext.tsx
Normal file
27
src/contexts/budget/BudgetContext.tsx
Normal file
@@ -0,0 +1,27 @@
|
||||
|
||||
import React, { createContext, useContext, ReactNode } from 'react';
|
||||
import { BudgetContextType } from './types';
|
||||
import { useBudgetState } from './useBudgetState';
|
||||
|
||||
// Context 생성
|
||||
const BudgetContext = createContext<BudgetContextType | undefined>(undefined);
|
||||
|
||||
// Context Provider 컴포넌트
|
||||
export const BudgetProvider: React.FC<{ children: ReactNode }> = ({ children }) => {
|
||||
const budgetState = useBudgetState();
|
||||
|
||||
return (
|
||||
<BudgetContext.Provider value={budgetState}>
|
||||
{children}
|
||||
</BudgetContext.Provider>
|
||||
);
|
||||
};
|
||||
|
||||
// Context 사용을 위한 Hook
|
||||
export const useBudget = () => {
|
||||
const context = useContext(BudgetContext);
|
||||
if (!context) {
|
||||
throw new Error('useBudget must be used within a BudgetProvider');
|
||||
}
|
||||
return context;
|
||||
};
|
||||
Reference in New Issue
Block a user