我的应用程序使用react-native-router-flux来决定向用户显示哪个组件取决于用户是否是
<Loading />__)<Home />__)<Welcome />__)实现这一目标的推荐方法是什么?
这个示例工作得很完美,只是它不使用react-native-router-flux。无论如何,要修改代码以使用react-native-router-flux
在下面的尝试中,在调用Actions.loading()函数之前调用render会产生一个错误,因为Actions还没有定义。if语句可以在render函数之后调用吗?
此外,每次更新道具时,都会导致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.connected是true ( receives连接到Meteor )时,this.props.user在从Meteor后端接收数据之前一直是null。但是,由于它最初被计算为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的错误/警告
下面是在通过关闭Meteor服务器将其与Meteor (DDP)服务器断开连接之前启动React一段时间后的Android日志输出。
使用console.log("<App /> render")时,每当render函数在Meteor的createContainer传递新的props后再次调用,就会触发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);发布于 2016-12-21 07:07:06
您可以通过使用初始支持来实现这一点,您可以从存储中读取数据,这些数据将指示用户是否登录,例如使用access_token。
然后使用initial={access_token} //,例如
但请记住,它将显示最后一幕与初始道具相等于真
// in componentDidMount
const currentUserAccessToken = store.getState().currentUserAccessToken;
<Scene
component={SignIn}
hideNavBar
initial={!currentUserAccessToken}
key="signIn"
title={I18n.t('sign_in')}
/>发布于 2016-12-21 03:02:06
您可以创建一个调度页面,并将您的逻辑放在那里。因此,只需添加调度作为一个场景,然后导航从那里。
return (
<Router>
<Scene key="root">
<Scene key="dispatch" component={Dispatch} hideNavBar={true} initial={true} />
<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>
)更新:
似乎流星正在把这些论点传递给道具。一旦添加了新的道具,componentWillUpdate()就不会被触发。相反,您应该将导航逻辑放在Actions中的componentWillReceiveProps(nextProps)中,如下所示:
componentWillReceiveProps(nextProps) {
if(nextProps.status.connected == false) {
Actions.loading();
} else {
console.log('meteor.user(): ', Meteor.user())
if (this.props.user !== null) {
Actions.home();
} else {
Actions.welcome();
}
}
}然后,您的导航逻辑应该在您的登录功能完成后触发。这应该能起作用。
https://stackoverflow.com/questions/41253572
复制相似问题