Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

87
Views
useEffect not call

I'm currently having a problem with a React app. I have a high level useEffect which is not being called. I have another project with a similar code and it works on it. Normally i should have my useEffect called every time i make an HTTP call from my app but nothing happens.

Does anyone have any idea who might be blocking?

Thanks a lot in advance for your help

My app.js

const App = () => (
  <Provider store={store}>
    <AuthProvider>
      <QueryClientProvider client={QueryClient()}>
        <ConnectedRouter history={history}>
          <>
            <Header />
            <AppNavigation />
            <AuthModalError />
          </>
        </ConnectedRouter>
      </QueryClientProvider>
    </AuthProvider>
  </Provider>
);

My authProvider component that be called in app.js

const AuthProvider = ({ children }) => {
  const [go, setGo] = useState(false);

  const token = "987646543216548789";
  useAxiosToken(token);

  useEffect(() => {
    setGo(token !== undefined);
  }, [token]);

  if (!go) return null;
  console.log("Authenticated !! And now display application");

  return <IdentityProvider value={token}>{children}</IdentityProvider>;
};

AuthProvider.propTypes = { children: PropTypes.node };

export default AuthProvider;

And finally the file which contains the useEffect which does not fire.

const removeTokenInterceptor = (id: number | null) => {
  if (id !== null) axios.interceptors.request.eject(id);
};

const addTokenInterceptor = (token: string) =>
  axios.interceptors.request.use((requestConfig) => ({
    ...requestConfig,
    headers: { ...requestConfig.headers, Authorization: `${token}` },
  }));

const useAxiosToken = (token: string | undefined): void => {
  const interceptor = useRef<number | null>(null)

  useEffect(() => {
    if (token) {
      interceptor.current = addTokenInterceptor(token);
    }
    console.log("Call useEffect in useAxiosToken file");
    return () => removeTokenInterceptor(interceptor.current);
  }, []);
};

export default useAxiosToken;

Pedro.

about 4 years ago · Santiago Trujillo
1 answers
Answer question

0

If you want your useEffect to be invoked each time something changes, you need to specify that "something" as a dependency of useEffect:

useEffect(() => {
    if (token) {
        interceptor.current = addTokenInterceptor(token)
    }
    console.log('Call useEffect in useAxiosToken file')
    return () => removeTokenInterceptor(interceptor.current)
}, [token])

Note the last argument of useEffect. It has [token] in it.

about 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!