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

258
Views
How to use Conditional Rendering and Redux in App js file?

I am trying to implement authentication flow with Redux in my app, I am using conditional rendering in my App.js file to show either app screen or authentication screen.

App.js:

    <Provider store={store} >
      <NavigationContainer>
        {store.getState().auth.initializing && <Initializing />}
        {store.getState().auth.isLoggedIn && (
          <AppNavigator/>
        )}
        {!store.getState().auth.initializing &&
          !store.getState().auth.isLoggedIn && (
          <AuthenticationNavigator/>
        )}
      </NavigationContainer>
    </Provider>

The problem is it doesn't react on changes in the state, when I press Login button on the login screen even though redux refreshes the state, the conditional rendering in App.js doesn't react anyhow and I still see login screen. If I use useState with isUserLoggedIn flag in the App.js, and pass callback to the components it works, but not with Redux.

Please help, what do I do wrong?

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

0

The Redux store.getState is only a function that returns the current state, it doesn't subscribe to changes to the store object. This only works when the component rendering your conditionals is rerendered.

What you can do is either use the subscribe method to subscribe to changes in the store, caching the state locally:

const [auth, setAuth] = React.useState(() => store.getState().auth);

useEffect(() => {
  const unsubscribe = store.subscribe(() => {
    setAuth(store.getState().auth);
  });

  return unsubscribe;
}, []);

...

<NavigationContainer>
  {auth.initializing && <Initializing />}
  {auth.isLoggedIn && (
    <AppNavigator/>
  )}
  {!.auth.initializing &&
    !.auth.isLoggedIn && (
    <AuthenticationNavigator/>
  )}
</NavigationContainer>

Or you and use the useSelector from react-redux to effectively do the same thing:

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

...

<NavigationContainer>
  {auth.initializing && <Initializing />}
  {auth.isLoggedIn && (
    <AppNavigator/>
  )}
  {!.auth.initializing &&
    !.auth.isLoggedIn && (
    <AuthenticationNavigator/>
  )}
</NavigationContainer>

Note that both of these solutions absolutely require the Redux store Provider to be moved higher in the ReactTree so that this component can consume the redux context and access the store's value.

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!