//index.js
ReactDOM.render(
<React.StrictMode>
<Router>
<App />
</Router>
</React.StrictMode>,
document.getElementById("root")
);
[<BrowserRouter>](https://reactrouter.com/web/api/BrowserRouter)
[<HashRouter>](https://reactrouter.com/web/api/HashRouter)
[<MemoryRouter>](https://reactrouter.com/web/api/MemoryRouter)
[<NativeRouter>](https://reactrouter.com/native/api/NativeRouter)
[<StaticRouter>](https://reactrouter.com/web/api/StaticRouter)
<Router>
/*기본 주소에 접속하면 보이는 태그*/
<Route exact path="/">
<h1 style={{ color: "#000000" }}>STAYC</h1>
<nav>
/*ol태그를 클릭하면 /뒤의 주소가 sieun으로 변경됨*/
<Link to="/sieun">
<ol>시은</ol>
</Link>
/*///////////////////////////////////*/
</nav>
<h2 style={{ color: "#000000" }}>멤버 이름 클릭</h2>
/*기본 주소에 접속하면 보이는 태그*/
/*기본주소/seeun으로 접속하면 보이는 태그*/
<Route exact path={"/seeun"}>
<Article member={seeun}></Article>
</Route>
/*기본 주소/seeun으로 접속하면 보이는 태그*/
</Router>
basename="/ko"
)path="/"
의 경우 모든 path에 매칭되므로 exact키워드를 추가하거나 가장 아래로 내림from="/"to="/login"
useEffect(()=>{
if(!validateFunc()){
history.push("login");
}
}, [] )
⇒ Private Route와 query String, URLSearchParams API는
나중에 간단하게 회원가입 폼을 만들어보면서 다시 포스팅 할 예정이다.
→ 단 이렇게하면 모든 페이지에서 404 페이지가 함께 나타날 수 있다.
그래서 **Switch
를 이용하여 많은 라우트중에 하나만** 선택 되게 해야한다.
<Router>
**<Switch>**
<Route exact path="/">
<h1 style={{ color: "#000000" }}>STAYC</h1>
</Route>
<Route exact path="/sieun">
<Article member={sieun}></Article>
</Route>
<Route exact path={"/seeun"}>
<Article member={seeun}></Article>
</Route>
**<Route>
<NotFoundPage></NotFoundPage>
</Route>
</Switch>**
</Router>
'/'
'/멤버'
import { BrowserRouter as Router, Switch, Route, Link } from "react-router-dom";
import "./App.css";
function Article({ member }) {
return (
<div>
<h2 style={{ color: "#000000" }}>{member.name}</h2>
<img src={member.img} style={{ width: 200, height: 500 }} />
<br />
<Link to="/">메인화면</Link>
</div>
);
}
function App() {
const sieun = {
name: "시은",
img: "http://static.inews24.com/v1/1504f905239326.jpg",
};
const sumin = {
name: "수민",
img: "http://talkimg.imbc.com/TVianUpload/tvian/TViews/image/2021/09/06/23185b44-ce33-4310-895b-e37888703a5c.jpg",
};
const seeun = {
name: "세은",
img: "https://newsimg.sedaily.com/2020/11/12/1ZADOJIIDW_1.jpg",
};
function NotFoundPage() {
return (
<div>
<h1 style={{ color: "#000000" }}>잘못된 경로!</h1>
</div>
);
}
return (
<div className="App">
<header className="App-header">
<Router>
<Switch>
<Route exact path="/">
<h1 style={{ color: "#000000" }}>STAYC</h1>
<nav>
<Link to="/sieun">
<ol>시은</ol>
</Link>
<Link to="/sumin">
<ol>수민</ol>
</Link>
<Link to="/seeun">
<ol>세은</ol>
</Link>
</nav>
<h2 style={{ color: "#000000" }}>멤버 이름 클릭</h2>
</Route>
<Route exact path="/sieun">
<Article member={sieun}></Article>
</Route>
<Route exact path="/sumin">
<Article member={sumin}></Article>
</Route>
<Route exact path={"/seeun"}>
<Article member={seeun}></Article>
</Route>
<Route>
<NotFoundPage></NotFoundPage>
</Route>
</Switch>
</Router>
</header>
</div>
);
}
export default App;
import React from "react";
import ReactDOM from "react-dom";
import "./index.css";
import App from "./App";
import reportWebVitals from "./reportWebVitals";
import { BrowserRouter as Router, Switch, Route, Link } from "react-router-dom";
ReactDOM.render(
<React.StrictMode>
<Router>
<App />
</Router>
</React.StrictMode>,
document.getElementById("root")
);
// If you want to start measuring performance in your app, pass a function
// to log results (for example: reportWebVitals(console.log))
// or send to an analytics endpoint. Learn more: https://bit.ly/CRA-vitals
reportWebVitals();
울 스터디장님 쩰로 멋져요!