DEV Community

Discussion on: Build a react-router clone from scratch

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
 
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
 
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!!