gpt4 book ai didi

javascript - 根据登录状态使用 React-Native-Router-Flux 显示不同的组件

转载 作者:行者123 更新时间:2023-11-29 10:33:18 35 4
gpt4 key购买 nike

我的应用正在使用 react-native-router-flux根据用户是否是

  • 登录,或等待应用连接到后端(显示<Loading />)
  • 登录(显示<Home />)
  • 未登录(显示<Welcome />)

实现该目标的推荐方法是什么?

这个例子很完美,只是它没有使用 react-native-router-flux .无论如何修改代码以使用react-native-router-flux

在我下面的尝试中,调用 Actions.loading()render 之前调用函数会出错,因为 Actions尚未定义。可以ifrender 之后调用语句功能?

此外,每次 Prop 更新时,都会导致重新渲染 App ,给出关于场景键如何已经存在的错误。

import React, { Component } from 'react';
import Meteor, { createContainer } from 'react-native-meteor';
import { Actions } from 'react-native-router-flux';

import Home from './components/home';
import Welcome from './components/welcome';
import Loading from './components/loading';
import settings from './config/settings';


Meteor.connect(settings.METEOR_URL);


const App = (props) => {

const { status, user, loggingIn } = props;

if (status.connected == false || loggingIn) {
// Render <Loading />
Actions.loading();
} else if (user !== null) {
// Render <Home />
Actions.home();
} else {
// Render <Welcome />
Actions.welcome();
}

return (
<Router>
<Scene key="root">
<Scene key="home" component={Home} title="Home" hideNavBar={true} />
<Scene key="welcome" component={Welcome} title="Welcome" hideNavBar={true} />
<Scene key="loading" component={Loading} title="Loading" hideNavBar={true} />
</Scene>
</Router>
)
}


export default createContainer(() => {
return {
status: Meteor.status(),
user: Meteor.user(),
loggingIn: Meteor.loggingIn()
};
}, App)

使用 Dispatch 的问题组件

根据 abeikverdi 的建议,我创建了以下 Dispatch包含显示组件之一的逻辑的组件 <Loading /> , <Home /><Welcome /> .

问题:this.props.status.connectedtrue (React Native 应用程序连接到 Meteor 后端),this.props.user总是 null在 React Native 应用程序从 Meteor 后端接收此数据之前的一段时间。但是因为它最初被评估为null , Action.welcome()将执行。 this.props.user 时,此行为不正确最终变成不null一两秒后,用户应该被重定向到 Actions.home()相反。

有什么想法吗?

export class Dispatch extends Component {
constructor(props) {
super(props);
}

componentWillUpdate() {
if(this.props.status.connected == false) {
Actions.loading();
} else {
console.log('meteor.user(): ', Meteor.user())
if (this.props.user !== null) {
Actions.home();
} else {
Actions.welcome();
}
}
}

render() {
return (
<View></View>
)
}
}


export default createContainer(() => {
return {
status: Meteor.status(),
user: Meteor.user(),
loggingIn: Meteor.loggingIn()
};
}, Dispatch);

日志中的错误/警告 react-native log-android

以下是 React Native 应用启动一段时间后,通过杀死 Meteor 服务器断开与 Meteor (DDP) 服务器的连接时的 Android 日志输出。

使用 console.log("<App /> render") ,似乎每当 render传递 new 后再次调用函数 props通过 meteor 的 createContainer , Key is already defined触发错误。

无论如何要摆脱这个错误/警告?

12-23 02:27:01.875 31197 19338 I ReactNativeJS: Running application "RNapp" with appParams: {"initialProps":{},"rootTag"
:1}. __DEV__ === true, development-level warning are ON, performance optimizations are OFF
12-23 02:27:01.891 31197 19338 I ReactNativeJS: render
12-23 02:27:01.995 31197 19338 I ReactNativeJS: Connected to DDP server.
12-23 02:27:01.999 31197 19338 I ReactNativeJS: Connected to DDP server.
12-23 02:27:02.012 31197 19338 I ReactNativeJS: render
12-23 02:27:02.013 31197 19338 I ReactNativeJS: Key home is already defined!
12-23 02:27:02.013 31197 19338 I ReactNativeJS: Key welcome is already defined!
12-23 02:27:02.013 31197 19338 I ReactNativeJS: Key loading is already defined!
12-23 02:27:02.013 31197 19338 I ReactNativeJS: Key root is already defined!
12-23 02:27:34.592 31197 19338 I ReactNativeJS: Disconnected from DDP server.
12-23 02:27:34.593 31197 19338 I ReactNativeJS: Disconnected from DDP server.
12-23 02:27:34.599 31197 19338 I ReactNativeJS: <App /> render
12-23 02:27:34.599 31197 19338 I ReactNativeJS: Key home is already defined!
12-23 02:27:34.599 31197 19338 I ReactNativeJS: Key welcome is already defined!
12-23 02:27:34.599 31197 19338 I ReactNativeJS: Key loading is already defined!
12-23 02:27:34.599 31197 19338 I ReactNativeJS: Key root is already defined!
12-23 02:27:34.609 31197 19338 I ReactNativeJS: <Loading /> render
12-23 02:27:35.603 31197 19338 I ReactNativeJS: Disconnected from DDP server.
12-23 02:27:35.613 31197 19338 I ReactNativeJS: <App /> render
12-23 02:27:35.613 31197 19338 I ReactNativeJS: Key home is already defined!
12-23 02:27:35.613 31197 19338 I ReactNativeJS: Key welcome is already defined!
12-23 02:27:35.613 31197 19338 I ReactNativeJS: Key loading is already defined!
12-23 02:27:35.613 31197 19338 I ReactNativeJS: Key root is already defined!
12-23 02:27:45.599 31197 19338 I ReactNativeJS: Disconnected from DDP server.
12-23 02:27:45.616 31197 19338 I ReactNativeJS: <App /> render
12-23 02:27:45.616 31197 19338 I ReactNativeJS: Key home is already defined!
12-23 02:27:45.616 31197 19338 I ReactNativeJS: Key welcome is already defined!
12-23 02:27:45.616 31197 19338 I ReactNativeJS: Key loading is already defined!
12-23 02:27:45.616 31197 19338 I ReactNativeJS: Key root is already defined!

App组件

export class App extends Component {

constructor(props) {
super(props);
}

componentWillReceiveProps(nextProps) {
if (nextProps.status.connected == false) {
Actions.loading();
} else {
if (nextProps.user !== null) {
Actions.home();
} else {
Actions.welcome();
}
}
}


render() {
console.log('<App /> render')
return (
<Router>
<Scene key="root">
<Scene key="home" component={Home} title="Home" hideNavBar={true} />
<Scene key="welcome" component={Welcome} title="Welcome" hideNavBar={true} />
<Scene key="loading" component={Loading} title="Loading" hideNavBar={true} />
<Scene key="profile" component={Profile} title="Home" hideNavBar={true} />
<Scene key="history" component={History} title="Home" hideNavBar={true} />
<Scene key="search" component={Search} title="Home" hideNavBar={true} />
</Scene>
</Router>
)
}

}


export default createContainer(() => {
return {
status: Meteor.status(),
user: Meteor.user(),
loggingIn: Meteor.loggingIn(),
};
}, App);

最佳答案

您可以通过使用 initial prop 来实现这一点,您可以从存储中读取指示用户是否已登录的数据,例如使用 access_token 。

然后你使用 initial={access_token}//例如

但请记住,它会显示初始属性等于 true 的最后一个场景

// in componentDidMount
const currentUserAccessToken = store.getState().currentUserAccessToken;
<Scene
component={SignIn}
hideNavBar
initial={!currentUserAccessToken}
key="signIn"
title={I18n.t('sign_in')}
/>

关于javascript - 根据登录状态使用 React-Native-Router-Flux 显示不同的组件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41253572/

35 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com