출저
- React 공식문서
- 위코드 수업자료
React-router
라이브러리를 설치하여 사용한다.React Router, and dynamic, client-side routing, allows us
to build a single-page web application
withnavigation without the page refreshing as the user navigates.
결국엔, SPA(single page application) 을 위해 페이지 이동을 좀 더 매끄럽게 하기위해 쓰는 third party library 이다.
npm install react-router-dom --save
When not using React Router, App is often the highest parent component in React apps. With React Router, however, the Router component needs to be the highest parent. This just lets all of the component use the power of Router, because as a parent, it passes down all of its props to its children, and thus the entire application.
라우터 컴포넌트 가장 부모위치에 있어야 하는데 그 위치에서 아래 자식 컴포넌트들에게 props 를 내려줘야 하기 때문이다. 이는 전체 어플리케이션에게 전달되는 것과 같은 의미이다.
아래와 같이 필요한 Router 와 경로에 추가해줄 컴포넌트를 import 해준 뒤, render()안에 Route라는 이름의 태그에 넣어준다.
ReactDOM.render(<Routes />, document.getElementById('root'));
Route 이동하는 방법은 두 가지가 있는데,
<Link to="/Signup">
<a>
: 외부 사이트로 이동하는 경우 사용.<Link>
: 프로젝트 내에서 컴포넌트로 이동하는 경우 사용.
<Link>
withRouterHOC