Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

how to switch rootscreen #6

Open
roycehe opened this issue Dec 1, 2022 · 2 comments
Open

how to switch rootscreen #6

roycehe opened this issue Dec 1, 2022 · 2 comments

Comments

@roycehe
Copy link

roycehe commented Dec 1, 2022

1 check if has token or not before screens show
2 if token use main tab, not use loginscreen
3 switch to main tab after login

@roycehe roycehe changed the title how to switch rootsreen how to switch rootscreen Dec 1, 2022
@kanzitelli
Copy link
Owner

Hey @roycehe! Let's take this - https://github.com/kanzitelli/rnn-starter/blob/master/App.tsx - as an example.
Say, we have stores.auth where you store your auth state. You can use it in App function to determine which root you want to use first. Then from Login screen, you will need to setRoot manually to show your tabs.

import {LogBox} from 'react-native';
import {Root, Screen, BottomTabs} from 'rnn-screens';

import {screens} from './src/screens';
import {Services} from './src/services';
import {Stores, stores} from './src/stores';
import {DesignSystem} from './src/utils/designSystem';
import SplashScreen from 'react-native-splash-screen';

LogBox.ignoreLogs(['Require', 'RCTBridge']);

export const beforeStart = async (): PVoid => {
  // 1. hydrate stores
  await Stores.hydrate();

  // 2. configure design system
  await DesignSystem.configure();

  // 3. init services
  await Services.init();

  // 4. hide splash screen
  SplashScreen.hide();
};

export const App = () =>
  Root(
    // we can use `stores` here as it was already inited in beforeStart functions
    stores.auth.isLoggedIn ?
      Screen(screens.get('Login')) : 
      BottomTabs([
        Screen(screens.get('Main')),
        Screen(screens.get('Playground')),
        Screen(screens.get('Settings')),
      ]),
  );

I didn't test this code, but it seems like a working piece.

@DavidKarasek
Copy link

This works when you launch the app. However, at least for me, the stores are not updating when state changes and the root stack never updates.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants