【发布时间】:2019-11-05 06:32:51
【问题描述】:
我只是想知道是否有更好的方法来处理我的 react-native 应用程序中的导航,使用 react-navigation 取决于 firebase 状态。
所以我有一个PreGameScreen,它显示了一个玩家列表,每个玩家都有一个就绪按钮,当按下该按钮时,他们的 firebase 文档中的ready 状态将更改为true - 一旦所有玩家都准备好了,我想要将它们导航到GameScreen。
因此,每个玩家都连接到 firebase 状态,因此他们将在 firebase 中的任何状态更改时收到回调。
我目前的解决方案是在 React Component 渲染函数中,我有一些逻辑可以检查所有玩家是否准备好,如果准备好,则将它们导航到屏幕。
有没有更好的方法来做到这一点,例如有某种导航大脑组件?所以最终会有更多的屏幕,我想根据 firebase 状态来控制用户看到的屏幕。
下面是我如何设置 firebase Schema
Game Document
gameName: string
*players*: Firebase Collection
Player Document
playerName: string
ready: bool
这是我在渲染函数中的一些代码
render() {
const { gameData, currentPlayer, navigation, allPlayersAreReady } = this.props;
if(allPlayersAreReady) {
navigation.navigate('InRound');
return null;
}
return (
<View style={styles.page}>
<View><Text>Pre-Round Screen</Text></View>
<View><Text>{gameData.gameName}</Text></View>
<PlayersList/>
{!currentPlayer.ready &&
<ReadyButton/>
}
</View>
)
}
【问题讨论】:
标签: reactjs firebase react-native navigation react-navigation