TIL 57 | React router - route-props(component)

ym j·2021년 8월 17일
0

React

목록 보기
7/9
post-thumbnail

React-router inline component

react-router 공식 문서를 보던 도중 route에 inline으로 component props를 사용하는 방법은 성능적으로 문제가 생길 수 있다고 하기에, 관련 내용을 간단하게 정리해봤다.

route props - component

<Router>
  <Switch>
    <Route exact path="/mypage" component={Mypage} />
    <Route exact path="/mentorpage" component={MentorPage} />
  </Switch>
</Router>
  • 이전에 프로젝트를 진행하면서, inline 방식의 route props인 component를 사용하여 해당 컴포넌트에 대한 경로를 지정하였다. 하지만, 공식문서를 확인해보니 이는 성능상의 문제가 발생할 수 있다고 한다. 엄밀히 말하자면 component라는 router propsReact.createElement를 통해 해당 컴포넌트가 렌더링이 될때마다 기존 생성되었던 컴포넌트가 렌더링이 되는 것이 아닌 동일한 컴포넌트가 새롭게 계속 생성이 되는 것이다. 때문에 component 대신 renderchildren props를 사용하라고 한다.


router props - render

<Router>
  <Switch>
    <Route exact path="/mypage" render={() => <Mypage />} />
    <Route exact path="/mentorpage" render={() => <MentorPage />} />
  </Switch>
</Router>
  • 사용 방법은 위처럼 propsvalue에 렌더링하고자 하는 컴포넌트를 리턴하는 콜백함수를 넣어주면 된다.

  • component와 다르게 랜더링이 불필요 마운팅(마운팅이 될때마다 새로운 컴포넌트를 지속적으로 생성하는 것)이 일어나지 않게 된다.



Reference

profile
블로그를 이전하였습니다 => "https://jymini.tistory.com"

0개의 댓글