DEV Community

Zeyad Etman
Zeyad Etman

Posted on

usePageTitle custom reactjs hook

Liquid error: internal

Top comments (5)

Collapse
 
worsnupd profile image
Daniel Worsnup

Love this idea! Might it be better to store the original title before changing it, and then restore the original title in the effect cleanup?

Collapse
 
zcdunn profile image
Zack Dunn

Shouldn't title be in the useEffect dependency array so that it reruns the effect if title changes?

I agree with @worsnupd that you should probably restore the original title in the cleanup.

Collapse
 
worsnupd profile image
Daniel Worsnup

Agreed! I totally missed that. The effect should list title as a dependency.

Collapse
 
dylankilkenny profile image
Dylan Kilkenny

Can you explain whats going on here, specifically why the anonymous function is returned?

Collapse
 
zeyadetman profile image
Zeyad Etman • Edited

When you use this hook in a component, you want to set the title for the current component, You don’t have to set title for all of your components. So This anonymous function is fired when the componentWillUnmount() to set default title to the components that you don’t want to set a title to it.