I writing a simple React application with two components assigned to two urls:
App.tsx
-- foo.tsx it's url is "/foo"
-- bar.tsx it's url is "/bar"
In App.tsx, i have a navigator use useHistory.push to allow user jump between "/foo" and "/bar". user's name will be capture here and store in a Redux state as well.
In foo.tsx and bar.tsx, they're petty much same but loading different type of data from API. the code structure like:
const user = useSelector((state: RootState) => state.user)
useEffect(() => {
const api = new Api();
async function getData() {
const foo: any = await api.getFoo(user.foo)
dispatch(setFooData(foo))
}
}, [user.name])
What i had observed:
const user = useSelector((state: RootState) => state.user) ran again, useEffect lost the pervious dependence to compare so dependence was ignored.Or, useHistory.push just works in such way, when a url push into history, it will make useEffect run at once no matter how.
I'm asking this question because i did remembered the useEffect wont be invoked when i jump back from another url, but back to that time i just started use react and not sure how i did that, and the git history had been overwhelmed by my numerous commits :(
To sum up, what I want to do is: when I access /foo, all data load, and redux state all set; same process when I access another url /bar. the useEffect doesn't been invoked when i back to /foo from /bar, unless i dispatch a new user.name. it's intent to reduce the requests to API since i had already use Redux store to manage the data for user. An other reason is that i get to figure out my understanding on useHistory, useEffect and Redux is correct.
Thanks!