简体   繁体   English

JS 中的条件语句在以下代码中不起作用

[英]Conditional Statement in JS is not working in the below code

const myFunction = (props) => {
  let myCondition = false;
  console.log('-----****----',props);

  if(props && props.isAllowed) {
    myCondition = true;
  }
  
  if(someOtherCondition) {
    return(
      <li><Link className="vx_globalNav-links" to="#">Policies</Link>
        <ul>
          <li><Link className="vx_globalNav-links" to="/my-policy">My Policy</Link></li>
          { myCondition && (<li><Link className="vx_globalNav-links" to="/condition-policy">Condition Policy</Link></li>)}
  
        </ul>
      </li>
    );
  }
  return null;
};

The above code seems to be not working for condition based link to display on UI for "Condition policy" even though I get "true" for "myCondition" in the console log which is just logged before the return statement.上面的代码似乎不适用于在 UI 上显示“条件策略”的基于条件的链接,即使我在返回语句之前记录的控制台日志中得到“myCondition”的“true”。 Wondering What's wrong here.想知道这里出了什么问题。 Any help is really appreciated.非常感谢任何帮助。

It looks like you put a statement immediately after your if() statement, and so your block of code within {... } is not executed as part of your if statement.看起来您在if()语句之后立即放置了一条语句,因此{... }中的代码块不会作为if语句的一部分执行。

if(someOtherCondition)
    console.log('', myCondition);
  {
    return(

The curly braces must follow immediately after the if() statement, otherwise, as it is in this case, the console.log is executed when someOtherCondition is true.花括号必须紧跟在if()语句之后,否则,就像在本例中一样,当someOtherCondition为真时,将执行console.log And whatever is within {... } is executed anyway regardless.不管怎样, {... }中的任何内容都会被执行。

Please check the below link, If you are something is not right, then please comment.请检查下面的链接,如果您有什么不对的地方,请发表评论。

https://playcode.io/873851/ https://playcode.io/873851/

  • If you put a condition without { just after the if statement, then the rest of the code does not depend on the condition.如果你在 if 语句后面放一个不带{的条件,那么代码的 rest 不依赖于条件。

Your if statement syntax is wrong您的 if 语句语法错误

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

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