[Nuag] Today I Learned - #3 Usage of withRouter() in react-router-dom

in react-router •  6 years ago  (edited)

What's up guys, here is another "Today I Learned" session.

If you saw my first post of this series, you will remember I talked about a way to pass props down from a parent component to a Route@react-router (A child component).

Not long ago, I talked to my friend and he showed me a much cleaner way to complete this job. But what needed is a better understanding of the react-router-dom library. But no worries, you will get it once you see the explanation.

Without further ado, here is the solution:

Say, you want your component to receive RouterProps, but don't want to wrap it in <Route component={ChildComponent}> (which is exactly the way I talked about in the previous post).

You can just use withRouter function to connect the "ChildComponent" component to the router, without additional manipulations or JSX tags wrapping. This looks much cleaner!


withRouter function on react-router-dom documentation:

You can get access to the history object’s properties and the closest < Route>'s match via the withRouter higher-order component. withRouter will pass updated match, location, and history props to the wrapped component whenever it renders.


So, it seems like I get better at this topic by a little bit today. I encourage you, my friend, to do so.

Cheers!

Authors get paid when people like you upvote their post.
If you enjoyed what you read here, create your account today and start earning FREE STEEM!
Sort Order:  

Hello! Your post has been resteemed and upvoted by @ilovecoding because we love coding! Keep up good work! Consider upvoting this comment to support the @ilovecoding and increase your future rewards! ^_^ Steem On!

Reply !stop to disable the comment. Thanks!

Congratulations @nuagnorab! You have completed the following achievement on the Steem blockchain and have been rewarded with new badge(s) :

You made more than 11000 upvotes. Your next target is to reach 12000 upvotes.

Click here to view your Board of Honor
If you no longer want to receive notifications, reply to this comment with the word STOP

To support your work, I also upvoted your post!

Do not miss the last post from @steemitboard:

SteemFest3 and SteemitBoard - Meet the Steemians Contest

Support SteemitBoard's project! Vote for its witness and get one more award!