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

in #react-router5 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!

Sort:  

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!

吃了吗?新人吗?《steemit指南》拿一份吧,以免迷路; 另外一定要去 @team-cn 的新手村看看,超级热闹的大家庭。假如我的留言打扰到你,请回复“取消”。

To listen to the audio version of this article click on the play image.

Brought to you by @tts. If you find it useful please consider upvoting this reply.

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!

Coin Marketplace

STEEM 0.25
TRX 0.11
JST 0.033
BTC 62480.78
ETH 3045.78
USDT 1.00
SBD 3.91