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

145
Views
ReactJs: How to make root component rerender when a prop changes

I have this component NotificationToast that manages notifications.
I need to be always rendered when the user is authenticated.

So this is what I did:

class App extends Component {
  render() {
    const state = store.getState();
    const { isAuthenticated } = state.auth;

    return (
      <Provider store={store}>
        <BrowserRouter basename="/">    
          <Switch>
            <PublicRoute path="/register-page" component={RegisterPage} />
            <PrivateRoute path="/profile-page" component={ProfilePage} />
            <Redirect to="/home" />
          </Switch>
          {isAuthenticated && (
            <div>
              <NotificationToast />
            </div>
          )}
        </BrowserRouter>
      </Provider>
    );
  }
}

export default App;

The problem is that this does not work in the following scenario:

  1. The user has not logged-in.
  2. So he is not authenticated. So state.auth.isAuthenticated is false
  3. When he logs-in, state.auth.isAuthenticated gets set to true.
  4. But, the problem is that App does not detect this change and so it does not get rerendered.
  5. And so, NotificationToast does not get rendered.
  6. And so the notification system doesn't work.

This works when the user refreshes the page after he logs-in, because state.auth.isAuthenticated is true from the beginning.

This is because of this code:

if (localStorage.jwtToken) {
  // Set auth token header auth
  setAuthToken(localStorage.jwtToken);
  // Decode token and get user info and expiration
  const decoded = jwt_decode(localStorage.jwtToken);
  // Set User and is Authenticated
  store.dispatch(setCurrentUser(decoded));
  // Now if you reload a page, if the user has already logged-in you will still have his info in he state
  store.dispatch(clearCurrentProfile());

  // Check for expired token
  const currentTime = Date.now() / 1000;

  if (decoded.exp < currentTime) {
    store.dispatch(logoutUser());
    store.dispatch(clearCurrentProfile());
    // Redirect to login
    window.location.href = "/login";
  }
}

It checks if the user has already logged-in, and if he did it sets state.auth.isAuthenticated to true.

Any suggestions?

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

useStore is used to retrieve store. But such store access can only be used for store manipulation, like reducer replacement. When store is changed, component which access store this way is not updated.

Please use "useSelector"

const isAuthenticated = useSelector(state => state.auth)

about 4 years ago · Juan Pablo Isaza 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!