사용자에게 보이는 화면은 서버에서 준비해줌
index.js
import ReactDOM from "react-dom";
import { BrowserRouter } from "react-router-dom";
import App from "./App";
const rootElement = document.getElementById("root");
ReactDOM.render(
<BrowserRouter>
<App />
</BrowserRouter>,
rootElement
);
<Router path="주소 규칙" component={보여줄 컴포넌트} />
App.js
import React from "react";
import { Route } from "react-router-dom";
import Home from "./Home";
import About from "./About";
export default function App() {
return (
<div>
<Route path="/" component={Home} />
<Route path="/about" component={About} />
</div>
);
}
/about으로 들어가면 home, about화면 모두 나옴
App.js
import React from "react";
import { Route } from "react-router-dom";
import Home from "./Home";
import About from "./About";
export default function App() {
return (
<div>
<Route path="/" component={Home} exact={true}/>
<Route path="/about" component={About} />
</div>
);
}
< Link to="주소">내용< /Link>
import React from "react";
import { Link, Route } from "react-router-dom";
import Home from "./Home";
import About from "./About";
export default function App() {
return (
<div>
<Link to="/">home</Link>
<br />
<Link to="/about">about</Link>
<hr />
<Route path="/" component={Home} exact={true} />
<Route path="/about" component={About} />
</div>
);
}
파라미터 : /profiles/jjaa9292
쿼리 : /about?details=true
profile.js
import React from "react";
const data = {
a : {
name: 'kim',
description : '개발자'
},
b : {
name : 'hong',
description : '디자이너'
}
}
const Profile = ({match}) => {
const {username} = match.params;
const profile = data[username];
if(!profile){
return <div>"존재하지 않음"</div>
}
return(
<div>
<h3>{username}({profile.name})</h3>
<p>{profile.description}</p>
</div>
)
}
export default Profile;
import "./styles.css";
import React from "react";
import { Link, Route } from "react-router-dom";
import Home from "./Home";
import About from "./About";
import Profile from "./Profile";
export default function App() {
return (
<div>
<Link to="/">home</Link>
<br />
<Link to="/about">about</Link>
<hr />
<Route path="/" component={Home} exact={true} />
<Route path="/about" component={About} />
<Route path="/profile/:username" component={Profile} />
</div>
);
}
{
“pathname”: “/about”,
“search”: “?detail=true”,
“hash”: “”
}
/about?detail=true 이런 경로로 들어가면 됨
about.js
import qs from "qs";
import React from "react";
const About = ({location}) => {
const query = qs.parse(location.search, {
ignoreQueryPrefix:true
})
const showDetail = query.detail==='true';
return (
<div>
<h1>About</h1>
<p>소개 페이지</p>
{showDetail&&<p>detail값이 true</p>}
</div>
);
};
export default About;
Profiles.js
import React from 'react';
import { Link, Route } from 'react-router-dom';
import Profile from './Profile';
const Profiles = () => {
return (
<div>
<h3>유저 목록:</h3>
<ul>
<li>
<Link to="/profiles/a">a</Link>
</li>
<li>
<Link to="/profiles/b">b</Link>
</li>
</ul>
<Route
path="/profiles"
exact
render={() => <div>유저를 선택해주세요.</div>}
/>
<Route path="/profiles/:username" component={Profile} />
</div>
);
};
export default Profiles;
App.js
import React from "react";
import { Link, Route } from "react-router-dom";
import Home from "./Home";
import About from "./About";
import Profiles from "./Profiles";
export default function App() {
return (
<div>
<Link to="/">home</Link>
<br />
<Link to="/about">about</Link>
<br />
<Link to="/profiles">프로필</Link>
<hr />
<Route exact path="/" component={Home} />
<Route path="/about" component={About} />
<Route path="/profiles" component={Profiles} />
</div>
);
}
HistorySample.js
import React, { useEffect } from "react";
const HistorySample = ({ history }) => {
const goBack = () => {
history.goBack();
};
const goHome = () => {
history.push("/");
};
useEffect(() => {
console.log(history);
const unblock = history.block("정말 떠나실건가요?");
return () => {
unblock();
};
}, [history]);
return (
<div>
<button onClick={goBack}>뒤로가기</button>
<button onClick={goHome}>홈으로</button>
</div>
);
};
export default HistorySample;
App.js
import React from "react";
import { Link, Route } from "react-router-dom";
import Home from "./Home";
import About from "./About";
import Profiles from "./Profiles";
import HistorySample from "./HistroySample";
const App = () => {
return (
<div>
<ul>
<li>
<Link to="/">홈</Link>
</li>
<li>
<Link to="/about">소개</Link>
</li>
<li>
<Link to="/profiles">프로필 목록</Link>
</li>
<li>
<Link to="/history">예제</Link>
</li>
</ul>
<hr />
<Route path="/" exact={true} component={Home} />
<Route path="/about" component={About} />
<Route path="/profiles" component={Profiles} />
<Route path="/history" component={HistorySample} />
</div>
);
};
export default App;
WithRouterSample.js
import React, { useEffect } from "react";
import { withRouter } from "react-router-dom";
const WithRouterSample = ({match, history, location}) => {
return(
<div>
<h4>location</h4>
<textarea value={JSON.stringify(location, null, 2)} readOnly />
<h4>match</h4>
<textarea value={JSON.stringify(match, null, 2)} readOnly />
<button onClick={() => history.push('/')}>홈으로</button>
</div>
)
}
export default withRouter(WithRouterSample);
Profiles.js
import React from "react";
import { Link, Route } from "react-router-dom";
import Profile from "./Profile";
import WithRouterSample from "./WithRouterSample";
const Profiles = () => {
return (
<div>
<h3>유저 목록:</h3>
<ul>
<li>
<Link to="/profiles/a">a</Link>
</li>
<li>
<Link to="/profiles/b">b</Link>
</li>
</ul>
<Route
path="/profiles"
exact
render={() => <div>유저를 선택해주세요.</div>}
/>
<Route path="/profiles/:username" component={Profile} />
<WithRouterSample />
</div>
);
};
export default Profiles;
Profile.js
import React from "react";
import { withRouter } from "react-router-dom";
import WithRouterSample from "./WithRouterSample";
const data = {
a: {
name: "kim",
description: "개발자"
},
b: {
name: "hong",
description: "디자이너"
}
};
const Profile = ({ match }) => {
const { username } = match.params;
const profile = data[username];
if (!profile) {
return <div>"존재하지 않음"</div>;
}
return (
<div>
<h3>
{username}({profile.name})
</h3>
<p>{profile.description}</p>
<WithRouterSample />
</div>
);
};
export default withRouter(Profile);
import React from "react";
import { Link, Route, Switch } from "react-router-dom";
import Home from "./Home";
import About from "./About";
import Profiles from "./Profiles";
import HistorySample from "./HistroySample";
const App = () => {
return (
<div>
<ul>
<li>
<Link to="/">홈</Link>
</li>
<li>
<Link to="/about">소개</Link>
</li>
<li>
<Link to="/profiles">프로필 목록</Link>
</li>
<li>
<Link to="/history">예제</Link>
</li>
</ul>
<hr />
<Switch>
<Route path="/" exact={true} component={Home} />
<Route path="/about" component={About} />
<Route path="/profiles" component={Profiles} />
<Route path="/history" component={HistorySample} />
<Route
render={({ location }) => (
<div>
<h2>페이지가 존재하지 않음</h2>
<p>{location.pathname}</p>
</div>
)}
/>
</Switch>
</div>
);
};
export default App;