简体   繁体   English

Javascript React组件的条件菜单项显示

[英]Javascript react component conditional menu item showing

I have a react component and I've added 2 links. 我有一个react组件,并且添加了2个链接。

One link will show the login link and the other with show the logout. 一个链接将显示登录链接,而另一个链接将显示注销。

I just don't want both to show so on componentDidMount I've added a condition which will hide either one or the other. 我只是不想两者都显示在componentDidMount上,所以我添加了一个条件,该条件将隐藏一个或另一个。

Here is the code: 这是代码:

class Navigation extends Component {

  componentDidMount() {

    let logged = true;

    if (logged) {
      document.getElementById('login').style.display = 'none';
    } else {
      document.getElementById('logout').style.display = 'none';
    }

    }
  }

  render() {

    return (
      <div>
        <ul>
          <li id="login"><a href="#login">Login</a></li>
          <li id="logout"><a href="#logout">Logout</a></li>
        </ul>
      </div>
    );
  }
}

export default Navigation;

The issue is not nothing is being hidden either way so it's not working. 问题不是两种方法都没有隐藏任何东西,所以它不起作用。

How can I fix this so I can get the condition to hide either one or the other? 我该如何解决这个问题,以便获得隐藏任何一个条件的条件?

I would say instead of directly modifying the DOM properties go with the React approach of conditional rendering like 我会说,与其直接修改DOM properties不如使用条件渲染的React方法,例如

class Navigation extends Component {

  render() {
    //Get the logged value from wherever you are getting it in the application.
    // Eg: let logged = true 
    return (
      <div>
        <ul>
          {logged ? (<li id="logout"><a href="#logout">Logout</a></li>) :  (<li id="login"><a href="#login">Login</a></li>) }

        </ul>
      </div>
    );
  }
}

export default Navigation;

You probably want to pass the logged variable to the Navigation component simply as props: <Navigation logged /> . 你可能想传递的logged变量的Navigation组件只是作为道具: <Navigation logged /> In that case, you could than write it this way: 在这种情况下,您可以这样写:

const Navigation = ({ logged }) => {
  <div>
    <ul>
      {!logged && <li id="login"><a href="#login">Login</a></li>}
      {logged && <li id="logout"><a href="#logout">Logout</a></li>}
    </ul>
  </div>
}

export default Navigation;

Instead of setting the style display: none and manipulating the DOM directly, use conditional rendering and render only one element on the basis of condition. 而不是设置样式display: none设置样式并直接操作DOM ,而是使用条件渲染,并根据条件仅渲染一个元素。 Use ternary operator applying conditions inside JSX . 使用三元运算符JSX内部应用条件。

Like this: 像这样:

 class Navigation extends React.Component { render() { let logged = true; //use actual value return ( <div> <ul> { logged ? <li id="logout"><a href="#logout">Logout</a></li> : <li id="login"><a href="#login">Login</a></li> } </ul> </div> ); } } ReactDOM.render(<Navigation/>, document.getElementById('app')) 
 <script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react.min.js"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react-dom.min.js"></script> <div id='app'/> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM