๋ถ๋ช ํ์ฌ์์ ๋๋ ค๋ดค์ ๋๋ ๋๋๋ฐ,
Uncaught Error: useRoutes() may be used only in the context of a <Router> component.
์ด ์๋ฌ๊ฐ ๋ด๋ค.
import { Route, Routes } from "react-router-dom";
import Home from "./pages/Home";
import Start from "./pages/PJTest/Start";
import Question from "./pages/PJTest/Question";
import Result from "./pages/PJTest/Result";
const App = () => {
return (
<Routes>
<Route path="/" element={<Home />} />
<Route path="/PJTest" element={<Start />} />
<Route path="/PJTest/Question" element={<Question />} />
<Route path="/PJTest/Result" element={<Result />} />
</Routes>
);
};
export default App;
ํด๋น ์๋ฌ๋ Router๊ฐ ํ์ํ๋ค๋ ๋ป์ด๋ค.
import { Router, Route, Routes } from "react-router-dom";
import Home from "./pages/Home";
import Start from "./pages/PJTest/Start";
import Question from "./pages/PJTest/Question";
import Result from "./pages/PJTest/Result";
const App = () => {
return (
<Router>
<Routes>
<Route path="/" element={<Home />} />
<Route path="/PJTest" element={<Start />} />
<Route path="/PJTest/Question" element={<Question />} />
<Route path="/PJTest/Result" element={<Result />} />
</Routes>
</Router>
);
};
export default App;
๊ทธ๋์ Router๋ก Routes๋ฅผ ๊ฐ์ธ์ฃผ๊ณ react-router-dom์์ Router์ import ํ๋ค.
๊ทธ๋ฌ๋๋
Uncaught TypeError: Cannot read properties of undefined (reading 'pathname')
์ด ์๋ฌ๊ฐ ๋ด๋ค.
๊ตฌ๊ธ๋งํด์ ์ฐพ์๋ณด๋ค๊ฐ, ํด๊ฒฐํ ์ ์์๋ค.
import { BrowserRouter as Router, Route, Routes } from "react-router-dom";
import ๋ถ๋ถ์ ์ด๋ ๊ฒ ๋ฐ๊ฟ์ฃผ์๋๋ ๋์ด์ ์๋ฌ๊ฐ ๋์ง ์์๋ค.
์ฐพ์๋ณด๋ ๋ผ์ฐํฐ๋
- BrowserRouter
- HashRouter
์ปดํฌ๋ํธ๋ฅผ ๊ฐ์ฅ ๋ง์ด ์ฌ์ฉํ๋ค๊ณ ํ๋ค.
React BrowserRouter์ HashRouter์ ์ฐจ์ด์ ์ ๋ฆฌ
๋ฆฌ์กํธ๋ฅผ ๊ณต๋ถ์ค์ธ ๋ฆฌ๋ฆฐ์ด๊ฐ ์ ๋ฆฌํด๋ณธ React Router, ํนํ BrowserRouter ์ HashRouter์ ์ฐจ์ด์ ์ ๋ํด์ ์ ๋ฆฌํ ๊ธ์ ๋๋ค. React Router React SPA(Single Page Application) ๊ตฌํ์ ๊ฐ์ฅ ๋ง์ด ์ฌ์ฉ ๋๋ 'rea..
worker-k.tistory.com
์ฐจ์ด์ ์ ์ฐพ์๋ดค๋๋ฐ, ์ฌ์ค ์์ง ๊ฐ์ ์ ์จ๋ค..
'Programming > React' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[ React ] aggrid ์ ํ๋์ด ์๋ ํ ์ ํ ํด์ (0) | 2022.09.28 |
---|---|
[ React ] aggrid ์๊ณ, ํฉ๊ณ row ์ถ๊ฐํ๊ธฐ (0) | 2022.08.22 |
[ React ] ํ์ ์คํฌ๋ฆฝํธ react-icons, styled-components ๋ชจ๋ ์ค์น (0) | 2022.08.03 |
[ React ] any[] ํ์ length๊ฐ undefined๋ก ๋จ๋ ํ์ ํด๊ฒฐ (0) | 2022.07.22 |
[ React ] ๋ฌธ์์ด ์นํ ์ฌ๋ฌ ๊ฐ (replace ํจ์) (0) | 2022.07.19 |