简体   繁体   English

如何在反应组件中编写 If 语句?

[英]How do I write a an If statement in a react component?

I am new to react and I am using a carbon react library I am just wondering how can I put an if statement in map function.我是新手,我正在使用碳反应库,我只是想知道如何在 map 函数中添加 if 语句。

It just writes the if statement in like it is normal string.它只是像普通字符串一样编写 if 语句。

<StructuredListBody>
{matchingResults?.map((X, i) =>
<StructuredListRow}>
{if (X?.status.includes(Working)){
<StructuredListCell>{X?.status}</StructuredListCell>}
else 
{<StructuredListCell>{X?.checking}</StructuredListCell>}};
</StructuredListRow>
)}
</StructuredListBody>

--- additional information I simplified the code but I am looking at cant use a ternary operator because there will be more than 2 conditions. --- 附加信息 我简化了代码,但我认为不能使用三元运算符,因为会有超过 2 个条件。

It is more like this I still don't get how to return a specific component in a component.更像是这样我仍然不知道如何在组件中返回特定组件。 I am trying to change the color.我正在尝试更改颜色。 The only thing that came to mind is to use the same again唯一想到的是再次使用相同的

    <StructuredListBody>
    {matchingResults?.map((X, i) =>
    <StructuredListRow key={`row-${row.id} -${i}`}>
    if (X?.status.includes(Working) {                                                
  <StructuredListCell key={`cell-${row.id}} noWrap style={{paddingBottom: '.5rem', color: 'red'}}>
    {X?.status}
    </StructuredListCell>}
    else 
    {<StructuredListCell key={`cell-${row.id} noWrap style={{paddingBottom: '.5rem', color: 'yellow'}}>
    {X?.status}
    </StructuredListCell>}
     )}
     </StructuredListBody>
 

As the only difference in rendering is the content in the StructuredListCell component.由于渲染的唯一区别是StructuredListCell组件中的内容。 The easiest way for your case would be to use the ternary operator (?), additionally the key props need to be unique, I used the index here, but you should use something unique in your data structure.对于您的情况,最简单的方法是使用三元运算符 (?),此外,关键道具必须是唯一的,我在这里使用了索引,但您应该在数据结构中使用唯一的东西。

        <StructuredListBody>
            {matchingResults.map((X, i) => (
                <StructuredListRow key={i}>
                    <StructuredListCell>{X?.status.includes(Working) ? X?.status : X?.checking}</StructuredListCell>
                </StructuredListRow>
            ))}
        </StructuredListBody>

Read more about Conditional Rendering in React阅读更多关于React 中的条件渲染

There are a couple of ways to have if statements in the render part of a react component.有几种方法可以在反应组件的渲染部分使用 if 语句。 You can conditionally render different components based on some logic like below:您可以根据以下逻辑有条件地渲染不同的组件:

<StructuredListBody>
    {matchingResults?.map((X, i) => {
        if (X?.status.includes(Working)) {
            return (
                <StructuredListRow>
                    <StructuredListCell>{X?.status}</StructuredListCell>
                </StructuredListRow>
            );
        } else {
            return (
                <StructuredListCell>{X?.checking}</StructuredListCell>
            );
        }
    })}
</StructuredListBody>

OR you could do the logic inside the component:或者您可以在组件内部执行逻辑:


<StructuredListBody>
    {matchingResults?.map((X, i) => (
        <StructuredListCell>
            {X?.status.includes(Working) ? X?.status : X?.checking}
        </StructuredListCell>
    ))}
</StructuredListBody>

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

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