我有一个带有某些组件的本地化项目。现在,我有一个使用此代码的导航助手:
const AppNavigator = createStackNavigator({
Login: {screen: Login},
SinglePage: {screen: SinglePageChat},
Home: {
screen: createBottomTabNavigator({
Home: {screen: ChatHome},
ChatPage: {screen: ChatPage},
Settings: {screen: Settings},
}, {
tabBarOptions: {
style: {
backgroundColor: "#eff0f1",
borderTopWidth: 1,
borderTopColor: "#D3D3D3"
},
indicatorStyle: {
backgroundColor: "red"
}
},
lazy: false,
navigationOptions: {
header: null
}
})
}
}, {
initialRouteName: 'Login',
headerMode: 'float', // screen for Android
});
// below options foreach page and the export default
您可以看到我的主页是Login。现在,用户登录,在这里我检查他是否有令牌,然后如果是,我将他重定向到主屏幕。在这里,我将用户连接到我的socket.io,它可以正常工作。
我还有一个名为“ SinglePageChat”的页面,用户可以在其中聊天。现在我有一个问题。以下面的代码为例:
// in constructor
this.socket = SocketIOClient('http://217.***.***.***:3000'); // this is the connection to the socket
// the code below is in the componentDidMount()
this.socket.on('hasMessages', (data) => {
console.log(data);
});
它不起作用。但是,在ChatHome或ChatPage中,套接字可以完美工作。现在,我在SinglePageChat组件中需要此代码。
为什么该页面不起作用?关于导航器?我不明白为什么会这样做,希望我足够清楚地解释我的问题!如果您需要更多代码示例,请告诉我!