创建导航栏以执行操作

时间:2019-07-03 19:24:13

标签: javascript react-native

我正在学习使用React-Native,并且遇到这种问题。

我应该做的是为页面创建一个Navbar自定义,允许我在右边插入一个可见的文本,具体取决于用户的角色。 (特别是,该文本使您可以注销到特定的用户角色。)

目前,我已经为所有用户实现了此功能。

在放置路线和场景的页面(我使用Router-flux )中,我已初始化注销:

static logout() {
User.clearLoggedUser();
App.redirectLogout();
}



render() {
    return (
      <Router>
      <Scene
            key="homepage"
            component={Homepage}
            type="reset"
            leftTitle="Home"
            leftButtonTextStyle={{ color: "#ffffff" }}
            onLeft={() => Actions.authentication()}
            rightButtonTextStyle={{ color: "#ffffff" }}
            rightTitle="Logout"
            onRight={() => App.logout()}
            navigationBarStyle={{ backgroundColor: "#64c7c0" }}
          />

因此,在应用程序右上角的主页中,有注销信息(但这仅适用于所有角色,而不适用于某些角色)。

在主页中,我担任以下角色:

class Homepage extends Component {
    constructor(props) {
        super(props);
    }
    render() {
        const Roles = global.user.data.Person.Roles

因此,如果{Roles}等于“ Some Role”,则他不应单击(并查看)注销按钮。

我该怎么办?感谢您的帮助。

1 个答案:

答案 0 :(得分:0)

听起来您需要在渲染场景时实现更多的逻辑。尝试以下方法:

static logout() {
User.clearLoggedUser();
App.redirectLogout();
}

render() {
    return (

      // These variables could also be stored in the state, 
      // depending on when/if they change and the scope you need them in.

      let currentRole = global.user.data.Person.Roles; // Or wherever the 
                                                // current role is stored

      let isLoggedIn = // boolean value to hold if anyone is logged in or not

      <Router>
      <Scene
            key="homepage"
            component={Homepage}
            type="reset"
            leftTitle="Home"
            leftButtonTextStyle={{ color: "#ffffff" }}
            onLeft={() => Actions.authentication()}
            rightButtonTextStyle={{ color: "#ffffff" }}

            rightTitle={isLoggedIn ? "Logout " + currentRole : ""}

            onRight={isLoggedIn ? () => App.logout() : null}

            navigationBarStyle={{ backgroundColor: "#64c7c0" }}
          />

因此,您需要在右侧按钮上显示一些逻辑,以告知是否应该显示以及是否应该单击它。您还可以使用当前用户的角色创建一个变量“注销”按钮,但您需要访问该角色的名称要比“首页”高一个范围。

如果当前角色管理器已登录,则上面的代码将导致在右侧按钮上显示“注销管理器”,如果没有人登录,则右侧按钮将不显示任何内容,并且单击操作为空。