Refactor BudgetContext.tsx into smaller components and hooks to improve code readability and maintainability.
28 lines
767 B
TypeScript
28 lines
767 B
TypeScript
|
|
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;
|
|
};
|