Skip to content

Commit

Permalink
(#82) 🎨 design: μŠ€νƒ€λ””μ›€ μ „μ—­ μƒνƒœ 동기화λ₯Ό μœ„ν•œ Provider 및 μ»€μŠ€ν…€ ν›… μ œμž‘
Browse files Browse the repository at this point in the history
  • Loading branch information
inaemon committed Nov 29, 2024
1 parent 0897f2d commit 2a0e47a
Showing 1 changed file with 31 additions and 0 deletions.
31 changes: 31 additions & 0 deletions src/context/StadiumContext.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import React, { createContext, useState, useContext, ReactNode } from "react";
import { StadiumType } from "@/src/constants/ZoneData";

// 1. Context νƒ€μž… μ •μ˜
interface StadiumContextType {
selectedStadium: StadiumType | null;
setSelectedStadium: (stadium: StadiumType) => void;
}

// 2. κΈ°λ³Έκ°’ μ„€μ •
const StadiumContext = createContext<StadiumContextType | undefined>(undefined);

// 3. Context Provider μ»΄ν¬λ„ŒνŠΈ
export const StadiumProvider: React.FC<{ children: ReactNode }> = ({ children }) => {
const [selectedStadium, setSelectedStadium] = useState<StadiumType | null>(null);

return (
<StadiumContext.Provider value={{ selectedStadium, setSelectedStadium }}>
{children}
</StadiumContext.Provider>
);
};

// 4. Context μ‚¬μš©μ„ μœ„ν•œ μ»€μŠ€ν…€ ν›…
export const useStadiumContext = (): StadiumContextType => {
const context = useContext(StadiumContext);
if (!context) {
throw new Error("useStadiumContext must be used within a StadiumProvider");
}
return context;
};

0 comments on commit 2a0e47a

Please sign in to comment.