0
I have a variable called nameScreen that I need to change its value every time the location.path is changed (whenever there is a route change):
<Typography variant="h6" noWrap>
{nameScreen}
</Typography>
I tried something like:
const [nameScreen, setNameScreen] = useState('')
const location = useLocation();
useEffect(() => {
changeTitleHeader()
// eslint-disable-next-line react-hooks/exhaustive-deps
},[location.pathname])
const changeTitleHeader = () => {
if (location.pathname === '/home') {
setNameScreen('Welcome to home!')
}
else if(location.pathname === '/users') {
setNameScreen('Users')
}
else if(location.pathname === '/companies') {
setNameScreen('Companies')
}
}
I need every time my Location.pathname is changed, my nameScreen variable changes its value based on the route name.
Man useEffect() is not being called again when the location.pathname is changed. How can I fix this?