简体   繁体   English

为什么这个代码块没有在我的 JSX 中执行?

[英]Why is this code block not executing in my JSX?

I'm making a simple bit of JSX that returns different text based on the value of a variable passed in from the back end.我正在制作一个简单的 JSX,它根据从后端传入的变量的值返回不同的文本。 Although the card is displayed correctly up to and including the {irr} , the code block with the ifs is simply not executing.尽管卡片在{irr}之前正确显示,但带有 ifs 的代码块根本没有执行。 There are no errors in the build process or browser, and none of the console.log statements are logged either.构建过程或浏览器中没有错误,也没有记录任何 console.log 语句。

I've done this successfully a few times before, even copy-pasting some of the code from another (working) part of the project as the basis of this bit, and it's pretty simple so I'm not sure where to start trying to figure out what I've done wrong here.我之前已经成功完成了几次,甚至从项目的另一个(工作)部分复制粘贴一些代码作为这一位的基础,而且非常简单,所以我不知道从哪里开始尝试弄清楚我在这里做错了什么。

Any ideas?有任何想法吗?

        <Card style={{ width: "72rem" }} className="mt-3 mr-2 ml-2">
          <Card.Body>
            <Card.Title className={classes.card}>{cardData.company}</Card.Title>
            <Card.Text className={classes.card}>
              {cardData.company} was bought 5 years ago for 
              €{purchasePrice}.
              At this time, the equity contribution of the PE owner was
              €{totalCF} as the deal was leveraged with a
              €{debtFinancing} term loan. Post-transaction, the PE firm received the intererim Free Cash Flow to Equity plus the current selling price minus the loan repayment. The total cash flows for the PE firm resulted in a  
              {irr}
              {(() => {
                console.log("In");
                if (irr > 0.15) {
                  console.log("A");
                    return (
                      <span>
                      Given the IRR is above the 15% target, the PE client is likely to deepen the relationship with the advisory team. In other words, {advisorName}  has done a good job.
                      </span>
                    );
                  } else if (irr <= 0.1) {
                  console.log("B");
                    return (
                      <span>
                      Given the IRR is way below the 15% target, the PE client might stop working with its advisors,  {advisorName}.
                      </span>
                    );
                  } else {
                  console.log("C");
                    return (
                      <span>
                      Given the IRR is below the 15% target, the relationship between the PE client and its advisor,  {advisorName}, has deteriorated.
                      </span>
                    );
                  }
              })}
            </Card.Text>
          </Card.Body>
        </Card>

with this way you have just bringing function inside the jsx, also you should execute using parentheses.通过这种方式,您只需将 function 带入 jsx,您也应该使用括号执行。 But I want to recommend you define another function outside this block for the purpose of decreasing cognitive complexity.但我想建议您在此块之外定义另一个 function 以降低认知复杂性。

https://en.wikipedia.org/wiki/Immediately_invoked_function_expression https://en.wikipedia.org/wiki/Immediately_invoked_function_expression

For example例如

    {(()=>{
        // Your if blocks
    })()}

Paste it.粘贴它。

<Card style={{ width: "72rem" }} className="mt-3 mr-2 ml-2">
<Card.Body>
  <Card.Title className={classes.card}>{cardData.company}</Card.Title>
  <Card.Text className={classes.card}>
    {cardData.company} was bought 5 years ago for 
    €{purchasePrice}.
    At this time, the equity contribution of the PE owner was
    €{totalCF} as the deal was leveraged with a
    €{debtFinancing} term loan. Post-transaction, the PE firm received the intererim Free Cash Flow to Equity plus the current selling price minus the loan repayment. The total cash flows for the PE firm resulted in a  
    {irr}
    {(() => {
      console.log("In");
      if (irr > 0.15) {
        console.log("A");
          return (
            <span>
            Given the IRR is above the 15% target, the PE client is likely to deepen the relationship with the advisory team. In other words, {advisorName}  has done a good job.
            </span>
          );
        } else if (irr <= 0.1) {
        console.log("B");
          return (
            <span>
            Given the IRR is way below the 15% target, the PE client might stop working with its advisors,  {advisorName}.
            </span>
          );
        } else {
        console.log("C");
          return (
            <span>
            Given the IRR is below the 15% target, the relationship between the PE client and its advisor,  {advisorName}, has deteriorated.
            </span>
          );
        }
    })()}
  </Card.Text>
</Card.Body>
</Card>

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

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