Sunday, June 2, 2024
 Popular · Latest · Hot · Upcoming
36
rated 0 times [  39] [ 3]  / answers: 1 / hits: 17237  / 5 Years ago, wed, september 25, 2019, 12:00:00

I am looking for a solution to register the route change and apply new state using setState and useEffect. The current code below doesn't update functions of setState when the route is changed.



For example, I register the pathname of / with location.pathname === '/' within createContext, if the pathname is / the setState of isHome is registered true, however if pathname is /page-1 setState is registered false.



On browser reloads, onMount the state is correctly set, however on a route change using Link 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) on pathname / I get true, any other pathnames I get false, however, if I change route, the current isHome state remains previous, until I scroll and useEffect 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 with componentDidUpdate and register prevProps.location.pathname against props.location.pathname, however, my understanding is that this is no longer necessary with the useEffect hook.


More From » reactjs

 Answers
13

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]);

[#51626] Monday, September 16, 2019, 5 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
kristiano

Total Points: 652
Total Questions: 94
Total Answers: 108

Location: Suriname
Member since Sun, Jun 13, 2021
3 Years ago
;