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.
Junior developer, mostly web. I work with Java, Spring, Javascript, React, CSS and a little Rust on the side. Webdev is my preferred domain and I'm interested in technologies that push it forward.
Oldest comments (5)
Can you explain whats going on here, specifically why the anonymous function is returned?
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.
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?
Shouldn't
titlebe in theuseEffectdependency array so that it reruns the effect iftitlechanges?I agree with @worsnupd that you should probably restore the original title in the cleanup.
Agreed! I totally missed that. The effect should list title as a dependency.