简体   繁体   English

当新用户登录时,如何确保我的菜单刷新? 尝试显示正确的项目(管理员用户的管理员导航)

[英]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.

相关问题 如何确保在输入字段中输入新数字时脚本/<p> 标签刷新? - How can I make sure when I put a new number in input field the script/ <p> tag refreshes? 如果其他用户具有管理员角色,我如何才能使我的管理页面(用于从数据库中删除用户)不在列表中显示其他用户? - How can I make my admin page (for removing users from DB) not display other users in the list if they have an admin role? 管理员登录后如何访问网站的其他页面 - How can I reach other pages of website when admin logs in 我在管理面板中有bootstrap侧边栏菜单,当我单击并获取链接时,有两个项目处于活动状态,如图片所示,为什么? - i have bootstrap Sidebar menu in my admin panel, when i click and fetch a link then two items are active , like in picture, why? 如何在帐户超时时提醒管理员用户 - How do I alert admin users when their account has timed out 当显示带有javascript的项目时,如何确保我的目录列表按字母顺序排序? - How can I make sure that my directory listing sorts in alphabetical order when it displays the items with javascript? 如何让我的自定义工具栏按钮在 react-admin 中使用表单的验证功能? - How do I make my custom toolbar button use the form's validation function in react-admin? 如何使 wordpress 管理菜单默认折叠? - How to make wordpress admin menu collapsed by default? 如何让我的机器人在响应时不提及用户? - How do I make my bot not mention users when it responds? 尝试将填充的数组传递给我的res.render。 不确定如何进行适当的异步回调 - Trying to pass a populated array to my res.render. Not sure how to make a proper async callback
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM