[英]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.