DEV Community

Cover image for Build a react-router clone from scratch

Build a react-router clone from scratch

Thomas Cullen on March 10, 2021

React router is a package that I use in almost all of my projects. Not too long ago, Michael Jackson tweeted this. This made me curious as to how d...
Collapse
 
harshilparmar profile image
Harshil Parmar

Great Effort !! brother 😁
I don't have much experience with useCallback,
Can you please explain this
How callback will detect that setLocation is changed !!
setLocation is just setter funciton right.

const handleHashChange = useCallback(() => {
    setLocation(window.location.pathname);
  }, [setLocation]);
Enter fullscreen mode Exit fullscreen mode
Collapse
 
adnanaslam profile image
Adnan Aslam

useCallback() hook is used to capture the whole function just a way to optimize the performance like as memoisation.

useCallback() used in conjunction with useEffect() because it allows you to prevent the re-creation of a function.

I hope this information helps you in understanding the useCallback() hook in react.

@Cheers
adnanaslam

Collapse
 
harshilparmar profile image
Harshil Parmar

Thanks brother!!

Collapse
 
thomascullen profile image
Thomas Cullen

Thanks for pointing this out! You are correct! We don't need to pass setLocation here in the callback dependencies as it won't change. Will update the post now, thank you!

Collapse
 
evgenyartemov profile image
Evgeny Artemov

Great content and so few likes! That's because it's hard to understand in 5 mins :)

Collapse
 
thomascullen profile image
Thomas Cullen

Thanks! Yeah definitely agree it's a lot for a quick read. I am thinking about also accompanying the post with a video tutorial which might be easier to digest.

Collapse
 
dgiulian profile image
Diego Giuliani

Great post. I always wondered how React Routed worked internally, but never dived into the code. This article made it much clear to me.

Thanks

Collapse
 
thomascullen profile image
Thomas Cullen

Great to hear! Thanks for the comment.

Collapse
 
eecolor profile image
EECOLOR

Great article!

I noticed a common ‘mistake’ in your code. I wrote about it here: medium.com/@ewestra/great-article-...

Collapse
 
thomascullen profile image
Thomas Cullen

Great feedback! I'l be sure to update the post. Thank's for the comment!

Collapse
 
sadiosan profile image
Sadio Sangharé

Nice work. Thank's

Collapse
 
jagrati16 profile image
jagrati16

Great Post !

Can you please explain where params in params[index] is defined?

Thanks.

Collapse
 
thomascullen profile image
Thomas Cullen • Edited

🤦‍♂️ I was missing a line. Sorry about that! Have updated the post now. Thanks for the comment!

Collapse
 
brunowmoreno profile image
Bruno Queiroz

Awesome man, thanks a lot!

Collapse
 
thomascullen profile image
Thomas Cullen

You're welcome! Glad you liked the read :) Hoping to do similar posts for other popular packages.