npm install react-router-dom
import { HashRouter as Router, Routes, Route } from "react-router-dom";
path = "경로", element = {<보여줄 요소/>}
Router.js
import React, { useState } from "react";
import { HashRouter as Router, Routes, Route } from "react-router-dom";
import Auth from "../routes/Auth";
import Home from "../routes/Home";
function AppRouter() {
const [isLoggedIn, setIsLoggedIn] = useState(false);
return (
<Router>
<Routes>
{isLoggedIn ? (
<>
<Route exact path="/" element={<Home />}></Route>
</>
) : (
<Route exact path="/" element={<Auth />}></Route>
)}
</Routes>
</Router>
);
}
export default AppRouter;
⭐️ 참고
Fragment < >
많은 요소들을 render 하고 싶을 때 사용함.
⚠️ Switch 사용 오류 : 'Switch' is not exported from 'react-router-dom'
Switch가 Routes로 바뀌었으므로 Routes를 사용해주면 된다.