我正在使用react-native-meteor连接到Meteor服务器。当我运行下面的代码时,我会得到截图形式的输出。在第一次调用render方法之前,不会建立连接。我该怎么处理呢?我可以阻塞直到连接发生吗?( onConnection方法在react-native-meteor中不可用)
export default class App extends Component {
componentWillMount() {
console.log("GOing to connect");
Meteor.connect(SERVER_URL);
console.log("After connect : " + JSON.stringify(Meteor.status()));
}
render() {
console.log(Meteor.status());
console.log(JSON.stringify(Meteor.user()));
if (Meteor.status().status === "connected") {
if (Meteor.user()) {
return ( <
TestViewOne / >
);
} else {
return ( <
TestViewTwo / >
);
}
} else {
return ( < View > < /View>)
}
}
}

当我使用以下代码时,Meteor.user()仍然返回null。
Meteor.ddp.on('connected', function() {
console.log("on Connected : " + JSON.stringify(Meteor.user()));
localForceUpdate();
});
发布于 2019-02-23 12:52:57
在使用withTracker连接服务器之前,您需要将应用程序设置为呈现加载屏幕,这样主应用程序将不会进入假定已建立连接的部分:
class App extends Component {
constructor(props) {
super(props);
}
render() {
if (!this.appLaunched) {
if (!this.props.serverConnected || this.props.loggingIn) {
return <LoadingScreen />;
}
}
this.appLaunched = true; //this prevents the app reloading to front page everytime server reconnects
return (
<Root>
<AppContainer />
</Root>
);
}
}
export default withTracker(params => {
return {
loggingIn: Meteor.loggingIn(),
serverConnected: Meteor.status().connected
};
})(App);https://stackoverflow.com/questions/45043016
复制相似问题