简体   繁体   English

仅当用户登录多次调用 AppRegistry.registerComponent 时才反应本机加载应用程序

[英]react native load app only if user is logged in calling AppRegistry.registerComponent multiple times

i have a user protected app, i want to authenticate user before loading the main app.我有一个受用户保护的应用程序,我想加载主应用程序之前对用户进行身份验证。 so i was wondering is it possible to register more than one app and switch between them in runtime ?所以我想知道是否可以注册多个应用程序并在运行时在它们之间切换?

for example i can have 3 apps 1. for login, 2. for admins, 3. for users例如我可以有 3 个应用程序 1. 用于登录,2. 用于管理员,3. 用于用户

i register the three, and then run one of them based on condition,我注册了三个,然后根据条件运行其中一个,

AppRegistry.registerComponent('LoginApp',()=><LoginApp />);
AppRegistry.registerComponent('AdminApp',()=><AdminApp />);
AppRegistry.registerComponent('UserApp',()=><UserApp />);

i can see on https://facebook.github.io/react-native/docs/appregistry.html doc page many functions that seem useful, yet there is no documentation on how to use any of these functions :(我可以在https://facebook.github.io/react-native/docs/appregistry.html文档页面上看到许多看似有用的功能,但没有关于如何使用这些功能的文档:(

runApplication,unmountApplicationComponentAtRootTag seem like they can do what i want but how to implement this ? runApplication,unmountApplicationComponentAtRootTag 似乎他们可以做我想做的事,但如何实现呢?

You could something like this:你可以是这样的:

Register new component:注册新组件:

AppRegistry.registerComponent(routeName, component);

Unmount the component by tag:通过标签卸载组件:

AppRegistry.unmountApplicationComponentAtRootTag(AppRegistry.getAppKeys()?.indexOf(routeName));

The RootTag is the index from registered app keys. RootTag是来自已注册应用程序键的索引。


Documentation:文档:

https://reactnative.dev/docs/appregistry.html#registercomponent https://reactnative.dev/docs/appregistry.html#registercomponent

I'm afraid I cannot help you with how AppRegistry would handle this, but I think this is not the right approach to what you are trying to achieve.恐怕我无法帮助您了解AppRegistry如何处理这个问题,但我认为这不是您想要实现的正确方法。 I would recommend instead to have a conditional in a MainApp.js that renders the appropriate 'App' based on a certain condition (your state).我会建议在MainApp.js中有一个条件,它根据特定条件(您的状态)呈现适当的“应用程序”。 For example:例如:

class MainApp extends Component {
  constructor(){
    this.state = {type: 'login'}; // Say your login component is the default one
  }
  render() {
    switch(this.state){
         case 'login': return <LoginApp onLoginSuccessful={type => this.setState({type})}/>
         case 'admin': return <AdminApp/>
         case 'user': return <UserApp/>
   }
  }
}

Then, your index.(android|ios).js becomes simply:然后,您的index.(android|ios).js变得很简单:

AppRegistry.registerComponent('MainApp',()=>); AppRegistry.registerComponent('MainApp',()=>);

You can extend this approach to read the type value from AsyncStorage or any other persistent utility so that the type is permanently stored and your users do not need to log in again, but I think the example covers the basics.您可以扩展此方法以从 AsyncStorage 或任何其他持久实用程序读取type值,以便永久存储该类型并且您的用户无需再次登录,但我认为该示例涵盖了基础知识。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何解决React Native中的AppRegistry.registerComponent错误 - How to solve AppRegistry.registerComponent Error in React Native React Native-这两个应用程序之间有什么区别,其中一个正在使用`AppRegistry.registerComponent`而另一个没有使用? - React Native - What is the difference between those two apps in which one is using the `AppRegistry.registerComponent` and another don't? 由于初始化期间require()错误或无法调用AppRegistry.registerComponent,React应用程序biqComfortApp尚未注册。 - React Application biqComfortApp has not been registered.due to require() error during initialisation or failure to call AppRegistry.registerComponent React Native - 调用AppRegistry.runApplication时出错 - React Native - Error calling AppRegistry.runApplication React-native Android:调用AppRegistry.runApplication时出错 - React-native Android: Error calling AppRegistry.runApplication 在 react-native 中调用 Appregistry.runApplication 时出错 - Error calling Appregistry.runApplication in react-native 多次加载本机库 - Load a native library multiple times React-Native:模块AppRegistry不是注册的可调用模块 - React-Native: Module AppRegistry is not a registered callable module 在 React-Native 中调用 interstitialAd.load() 方法时应用程序崩溃 - App is crashing while calling interstitialAd.load() method in React-Native GetCurrent 用户仅返回来自 Firebase Google 应用的登录用户 - GetCurrent user return only logged user from Firebase Google app
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM