react-router-dom Switch component, NotFound

2021. 7. 27. 15:01React

<Switch>

  <Route path="/profile" component={Profile} />

  <Route path="/About:id" component={About} />

  <Route path="/About" component={About} />

  <Route path="/" exact component={Home} />

  <Route component{NotFoubd} />  

</Switch>

 

Switch component 작성방법

1. 좁은 범위의 컴포넌트부터 넓은 범위의 컴포넌트 순서로 작성한다.

  <Route path="/About:id" component={About} />  다음  <Route path="/About" component={About} />

 

2. Root경로에는 exact를 추가

3. NotFound는 제일 아래

'React' 카테고리의 다른 글

React Reconciliation  (0) 2021.07.30
react-router-dom Redirect  (0) 2021.07.27
react-router-dom에서 페이지 이동시 Link component 사용  (0) 2021.07.27
Dynamic Routing에서 props 가져올 때  (0) 2021.07.27
React State Handling  (0) 2021.07.24