[英]How do I make sure my menu refreshes when a new user logs in? Trying to show the proper items (admin navigation for admin users)
My SideMenu.js basically does a check to see if user is admin or not before building the proper routes to render, my issue is that when i sign out and sign back in, it keeps the same menu from the previous user.我的 SideMenu.js 基本上会在构建正确的渲染路径之前检查用户是否是管理员,我的问题是当我退出并重新登录时,它与前一个用户保持相同的菜单。
It looks something like this:它看起来像这样:
class SideMenu extends Component {
constructor () {
super();
this.state = {
isAdmin: false,
isLoading: true,
footer: [
{ label: 'Settings', url: 'settingsUrl', icon: settingsIcon, id: '1' },
{ label: 'Log Out', url: 'logOutUrl', icon: signoutIcon, id: '2' },
]
}
this.checkVerification();
}
checkVerification = () => {
var isLoggedIn = Firebase.isLoggedIn();
var isAdmin = Firebase.isAdmin();
Promise.all([ isLoggedIn, isAdmin ]).then((responses) => {
isLoggedIn = responses[0];
isAdmin = responses[1];
if(isLoggedIn){
if(isAdmin){
this.setState({
isAdmin: true,
isLoading: false,
routes: [
{ label: 'Screen1', url: 'screenUrl1', icon: screenIcon1, id: '1' },
{ label: 'Screen2', url: 'screenUrl2', icon: screenIcon2, id: '2' },
{ label: 'AdminScreen3', url: 'screenUrl3', icon: screenIcon3, id: '3' },
]
})
}else{
this.setState({
isLoading: false,
routes: [
{ label: 'Screen1', url: 'screenUrl1', icon: screenIcon1, id: '1' },
{ label: 'Screen2', url: 'screenUrl2', icon: screenIcon2, id: '2' },
]
})
}
}else{
this.props.navigation.navigate('Login')
}
})
}
Then I render the menu at the end:然后我在最后渲染菜单:
render () {
const { isLoading } = this.state;
return(
<View style={styles.container}>
{isLoading && this.renderLoading()}
{!isLoading && this.renderMenu()}
</View>
)
}
The Menu only loads once and never refreshes when a user signs out and back in with a different account.菜单仅加载一次,并且在用户注销并使用其他帐户重新登录时从不刷新。 I tried a bunch of checks in the sidemenu render but it resulted in functions being spammed and didnt work either way.我在 sidemenu 渲染中尝试了一堆检查,但它导致功能被垃圾邮件发送并且无论哪种方式都不起作用。 Then I found a post of someone mentioning to use StackActions.reset, which I did in my Login Screen, which works, the code is as follows:然后我发现有人提到使用StackActions.reset的帖子,我在我的登录屏幕中做了这个,它有效,代码如下:
const resetAction = StackActions.reset({
index: 0,
actions: [NavigationActions.navigate({ routeName: 'Init' })],
});
this.props.navigation.dispatch(resetAction);
This works, but I get the warning:这有效,但我收到警告:
'Warning: Can\'t perform a React state update on an unmounted component. '警告:无法对未安装的组件执行 React state 更新。 This is a no-op, but it indicates a memory leak in your application.这是一个无操作,但它表明您的应用程序中存在 memory 泄漏。 To fix, cancel all subscriptions and asynchronous tasks in %s.%s', 'the componentWillUnmount method', '\n in Login (at SceneView.js:9)要解决此问题,请在 Login (at SceneView.js:9) 中取消 %s.%s', 'componentWillUnmount method', '\n 中的所有订阅和异步任务
What's the proper way to go about refreshing my menu on new user? go 关于在新用户上刷新我的菜单的正确方法是什么? I've been trying to fix it for quite a while with no proper result.我一直在尝试修复它很长一段时间,但没有正确的结果。 I'm not sure if it helps to know that I load my SideMenu from my Init Page:我不确定知道我从初始化页面加载 SideMenu 是否有帮助:
import { AppRegistry, Dimensions, YellowBox } from 'react-native';
import { createDrawerNavigator , createAppContainer } from 'react-navigation';
import SideMenu from './pages/menu/SideMenu'
import Routes from './Routes';
const DrawerNav = createDrawerNavigator({
Screen: {
screen: Routes,
}
}, {
contentComponent: SideMenu,
drawerWidth: Dimensions.get('window').width/1.7,
overlayColor: 'rgba(0, 0, 0, 0.7)',
minSwipeDistance: 5,
});
const Init = createAppContainer(DrawerNav);
export default Init;
Any help would be greatly appreciated, thank you in advance.任何帮助将不胜感激,在此先感谢您。
I figured it out.我想到了。 First of all I had a bunch of promises that weren't getting resolved when signing out that were giving me errors, after resolving them properly, I ended up changing my logOut.js to:首先,我有一堆在退出时没有得到解决的承诺,这些承诺给了我错误,在正确解决它们之后,我最终将我的 logOut.js 更改为:
componentDidMount(){
Firebase.logOut().then((response) => {
const resetAction = StackActions.reset({
index: 0,
actions: [NavigationActions.navigate({ routeName: 'Login' })],
});
this.props.navigation.dispatch(resetAction);
})
}
That did the trick for me, when I tried that in my SideMenu.js when route == 'LogOut' it gave me warnings.这对我有用,当我在我的 SideMenu.js 中尝试 route == 'LogOut' 时它给了我警告。 It seems to be working this way though.不过,它似乎是这样工作的。 Hope this helps someone struggling with the same issue out if they come across this.如果他们遇到这个问题,希望这可以帮助那些在同样问题上苦苦挣扎的人。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.