在某些特定用户中隐藏菜单栏

时间:2018-10-18 08:18:15

标签: reactjs

如果用户不是管理员,我想隐藏菜单栏。如何有条件地渲染菜单栏?

{this.state.userName === 'admin' ?
                    <SubMenu className="side-sub-menu" key="6" style={style}
                             title={<a><img src="/assets/settings.png" alt="settings-img"/><span className="nav-text">Settings</span></a>}>
                        <Menu.Item>
                            <Link to='/companies'>
                                <img src="/assets/modules.png" alt="modules-img"/>
                                <span className="nav-text">Companies</span>
                            </Link>
                        </Menu.Item>
 </SubMenu>
                    : ''} 

2 个答案:

答案 0 :(得分:0)

使用逻辑 a = {'state': 1 , 'path': 2} b = tf.get_variable(name = 'my_variable' , shape=[batch_size, None]) state = { 'x' : a , 'y' : b } shape = { 'y' : tf.TensorShape([batch_size, None]) } tf.while_loop( cond, body, loop_vars=[state], shape_invariants=[shape], parallel_iterations=1, back_prop=False) 运算符代替使用条件(三元)运算符。

&&

答案 1 :(得分:0)

&&被视为short-circuit evaluation,即,如果左侧为假,则不评估右侧。您可以利用此功能来有条件地渲染组件。

您可以使用以下内容:

class Test extends React.PureComponent {
  render() {
    const { userName } = this.state
    const isAdmin = userName === 'admin'
    return (
      <div>
        {isAdmin && (
          <SubMenu
            className="side-sub-menu"
            key="6"
            style={style}
            title={
              <a>
                <img src="/assets/settings.png" alt="settings-img" />
                <span className="nav-text">Settings</span>
              </a>
            }
          >
            <Menu.Item>
              <Link to="/companies">
                <img src="/assets/modules.png" alt="modules-img" />
                <span className="nav-text">Companies</span>
              </Link>
            </Menu.Item>
          </SubMenu>
        )}
      </div>
    )
  }
}