dukDukz
[React] Router replace to 본문
import React from "react";
import { Routes, Route, Link, Navigate } from "react-router-dom";
import Home from "./pages/Home";
import Login from "./pages/Login";
function NoMatch() {
return (
<div>
<h2>페이지를 찾을 수 없습니다.</h2>
<p>
<Link to="/">홈으로</Link>
</p>
</div>
);
}
function Router() {
return (
<Routes>
<Route path="/login" exact={true} element={<Login />} />
<Route path="/" element={<Navigate replace to="/login" />} />
<Route path="/home" exact={true} element={<Home />} />
<Route path="*" element={<NoMatch />} />
</Routes>
);
}
export default Router;
1. '/' 로 들어왔을때 '/login' 으로 자동이동함.
2. 없는 링크로 접근하는 경우 NoMatch 를 리턴함.
'웹 개발 > React' 카테고리의 다른 글
[error] @parcel/transformer-js: Browser scripts cannot have imports or exports. (0) | 2023.03.02 |
---|---|
[React] (useRef 활용) 함수를 특정 페이지에서만 동작하지 않게 하기 (0) | 2023.02.21 |
Uncaught Error: Invariant failed: You should not use <Switch> outside a <Router> (1) | 2022.10.04 |
[React] Route - Redirect (0) | 2022.10.04 |
[React] Router render / component (0) | 2022.10.04 |