useEffect doesn't update state on route change
Solution 1
The effect you want is "When the location change then update my state", this is translated in useEffect
code like this :
useEffect(() => {
detectHome();
return () => {
detectHome();
};
}, [location]);
Solution 2
If you are using react-router you can subscribe to location change event in your useEffect:
import {browserHistory} from 'react-router';
...
useEffect(() => {
return browserHistory.listen(detectHome);
}, []);
...
This would subscribe your detectHome
function for location change on mount and unsubscribe it on unmount.
Darren
Updated on August 10, 2022Comments
-
Darren almost 2 years
I am looking for a solution to register the route change and apply new
state
usingsetState
anduseEffect
. The current code below doesn't update functions ofsetState
when the route is changed.For example, I register the
pathname
of/
withlocation.pathname === '/'
withincreateContext
, if thepathname
is/
thesetState
ofisHome
is registeredtrue
, however ifpathname
is/page-1
setState
is registeredfalse
.On browser reloads,
onMount
thestate
is correctly set, however on a route change usingLink
this does not. Also, please note that I am using Gatsby and in doing so, importing{ Link } from 'gatsby'
CreateContext.js
export const GlobalProvider = ({ children, location }) => { const prevScrollY = useRef(0); const [state, setState] = useState({ isHome: location.pathname === '/', // other states }); const detectHome = () => { const homePath = location.pathname === '/'; if (!homePath) { setState(prevState => ({ ...prevState, isHome: false })); } if (homePath) { setState(prevState => ({ ...prevState, isHome: true })); } }; useEffect(() => { detectHome(); return () => { detectHome(); }; }, [state.isHome]); return ( <GlobalConsumer.Provider value={{ dataContext: state, }} > {children} </GlobalConsumer.Provider> ); };
If I
console.log(state.isHome)
onpathname
/
I gettrue
, any other pathnames I getfalse
, however, if I change route, the currentisHome
state remains previous, until I scroll anduseEffect
applies.The point of registering the
isHome
state is to alter CSS per page.How can I update state with
useEffect
when changing route. Previously, I would have done this withcomponentDidUpdate
and registerprevProps.location.pathname
againstprops.location.pathname
, however, my understanding is that this is no longer necessary with theuseEffect
hook.