Movatterモバイル変換


[0]ホーム

URL:


Skip to content
DEV Community
Log in Create account

DEV Community

Cover image for React Router  - Route ( WTF Moment )
Santiago Correa
Santiago Correa

Posted on

     

React Router - Route ( WTF Moment )

Recently I was working on a project which consisted of books, and I realized that my whole web app was mounting and unmounting instead of the components re-rendering.

More details:

The project consisted of 2 pages.

Here's a quick wireframe I whipped up:
Alt Text

  1. Home
  2. Search

I used react-router to create the navigation between pages.

What I didn't know, until I further read their documentation, is that if you use the prop component in<Route> it uses React.createElement to create a new React element from the given component.

That means if you provide an inline function to the component prop, you would create a new component every render. This results in the existing component unmounting and the new component mounting instead of just updating the existing component.

This caused undesired behavior which was when I was on the search page and updated the App state it would refresh everything and I would lose the state I had in the Search page.

Instead, to just make sure the component updates and doesn't mount and unmount when there is an update,use render.

<Route  path='/'  exact  render={() => (    <Home     currentlyReading={this.state.currentlyReading}     read={this.state.read}    wantToRead={this.state.wantToRead}    updateBookShelf={(book, shelf) =>     this.updateBookShelf(book, shelf)} />  )}></Route>
Enter fullscreen modeExit fullscreen mode
<Route  path='/'  exact  render={() => (    <Search     allBooks={this.state.books}    updateBookShelf={(book, shelf) =>     this.updateBookShelf(book, shelf)} />  )}></Route>
Enter fullscreen modeExit fullscreen mode

Summary

  • Component unmounts and mounts a new component.
  • Render updates components, instead of unmounting and mounting.

Top comments(9)

Subscribe
pic
Create template

Templates let you quickly answer FAQs or store snippets for re-use.

Dismiss
CollapseExpand
 
limxingzhi profile image
XING
  • Joined
• Edited on• Edited

For anyone curious, this is the documentation for the specific mounting behavior mentioned by the author :reactrouter.com/web/api/Route/rend...

Which kinda makes sense doesn’t it. React aside, typical web based routing that serves different HTML pages based on the URL will definitely not retain your local state between routes unless you implement some sort of store or caching solution. The routing behavior of unmounting a component when the URL changes sort of fits into that narrative.

I am curious though, did you notice any performance difference between using therender prop VS the child node?

Edit:store or caching solution

CollapseExpand
 
scorreaui profile image
Santiago Correa
👋 Hi, I’m @santiago correa👀 I’m interested in dominating the world.... Just kidding just want to make the web a better and accessible place for all.

Hi there,

No i didn’t really notice any performance improvements between using component and render

CollapseExpand
 
hootdunk profile image
HootDunk
  • Joined

I'm so glad you made this post. I hit this same snag on a person project and never came to the realization. Good to know it was in the docs the whole time and that I probably should have looked harder!

CollapseExpand
 
scorreaui profile image
Santiago Correa
👋 Hi, I’m @santiago correa👀 I’m interested in dominating the world.... Just kidding just want to make the web a better and accessible place for all.

Yeah, I know. I was stuck for a little while as well, and that’s why I decided to share it. :)

CollapseExpand
 
amaggi profile image
Agustin Maggi
Developer • DevOps enthusiast • self-proclaimed designer
  • Location
    Argentina
  • Work
    Senior Software Engineer
  • Joined
• Edited on• Edited

Try next.js. With it you do avoid all of these potential problems.

CollapseExpand
 
scorreaui profile image
Santiago Correa
👋 Hi, I’m @santiago correa👀 I’m interested in dominating the world.... Just kidding just want to make the web a better and accessible place for all.

I usually use Next.js, but I like to try different things XD. I also have used gatsby and no prob either but that was more for a blogging site.

CollapseExpand
 
mainendra profile image
Mainendra
JavaScript developer and coding enthusiast.
  • Location
    Mississauga
  • Joined

Wouter is good alternative if you want to try.github.com/molefrog/wouter

CollapseExpand
 
scorreaui profile image
Santiago Correa
👋 Hi, I’m @santiago correa👀 I’m interested in dominating the world.... Just kidding just want to make the web a better and accessible place for all.

I’ll take a look in a future project, thank you.

CollapseExpand
 
anthonyfinix profile image
Anthony Finix
Student
  • Location
    Haryana,IN
  • Work
    Web Developer at LoftyTechnologies
  • Joined

Thanks a lot.

Are you sure you want to hide this comment? It will become hidden in your post, but will still be visible via the comment'spermalink.

For further actions, you may consider blocking this person and/orreporting abuse

👋 Hi, I’m @santiago correa👀 I’m interested in dominating the world.... Just kidding just want to make the web a better and accessible place for all.
  • Location
    Colombia
  • Work
    SSR Front End Engineer at Globant
  • Joined

More fromSantiago Correa

DEV Community

We're a place where coders share, stay up-to-date and grow their careers.

Log in Create account

[8]ページ先頭

©2009-2025 Movatter.jp