함수형 프로젝트를 진행하는데, 전역으로 관리할 state가 많지 않아서 useContext를 사용한다.
( 현재까지는 user 정보만 가지고 다니면 됨 )
폴더구조
src/hooks/TestContext.js
import { createContext } from "react";
export const TestContext = createContext(null);
import React, { useState } from "react";
import { BrowserRouter, Route, Routes } from "react-router-dom";
import routes from "./routes";
// test useContext
import { TestContext } from "./hooks/TestContext";
function App() {
const [user, setUser] = useState({
email: 'donnie.lee@email.kr',
name : 'donnie',
other : 'female'
})
return (
<>
<TestContext.Provider value={user}>
<BrowserRouter>
<Routes>
{routes.map((route, index) => (
<Route
key={index}
path={route.path}
exact={route.exact}
name={route.name}
element={<route.element />}
/>
))}
</Routes>
</BrowserRouter>
</TestContext.Provider>
</>
);
}
export default App;
import React, { useState } from "react";
import { BrowserRouter, Route, Routes } from "react-router-dom";
import routes from "./routes";
// test useContext
import { TestContext } from "./hooks/TestContext";
function App() {
const [user, setUser] = useState({
email: 'donnie.lee@email.kr',
name : 'donnie',
other : 'female'
})
return (
<>
<TestContext.Provider value={user}>
<BrowserRouter>
<Routes>
{routes.map((route, index) => (
<Route
key={index}
path={route.path}
exact={route.exact}
name={route.name}
element={<route.element />}
/>
))}
</Routes>
</BrowserRouter>
</TestContext.Provider>
</>
);
}
export default App;