简体   繁体   English

RectNative 中的多个条件同时显示两个条件

[英]Multiple Conditions in RectNative shows two conditions in the same time

component1 should show up if options is less than 4. component2 should show up if options is more than 4. component3 should show up if options is more than 7. Right now if my options array has less than 4 values it displays together component1 with component3.如果选项小于 4,组件1 应该显示。如果选项大于 4,组件2 应该显示。如果选项大于 7,组件3 应该显示。现在,如果我的选项数组的值小于 4,它会一起显示组件 1 和组件 3 . Why is that so?为什么呢?

<View>
{options.length < 4 && (
<Component1
 options={options}
/>
)}
{options.length > 4 && (
<Component2 options={options}/>
)}
{options.length < 7 && (
<Component3 options={options}/>
)}
</View>

try this,尝试这个,

<View>
{options.length < 4 && (
<Component1 options={options}/>
)}
{options.length > 4 && (
<Component2 options={options}/>
)}
{options.length > 7 && (     // change your code here
<Component3 options={options}/>
)}
</View>

Hi you can write like it你好,你可以这样写

 const renderComponents = (options) => {
    if (options.length < 4) {
        return (
            <Component1
                options={options}
            />
        )
    } else if (options.length < 4) {
        return (
            <Component2 options={options} />
        )
    } else if (options.length > 7) {
        return (
            <Component3 options={options} />
        )
    }
}

and call the renderComponents where you want to use并调用您要使用的 renderComponents

{renderComponents(options)}

hopefully it will be helpful for you希望对你有帮助

Try this code you need to check for multiple conditions if there is ant试试这个代码,如果有 ant,你需要检查多个条件

      <View>
        {options.length < 4 && 
          <Component1 options={options} />
        }
        {options.length > 4 && options.length < 7 && (
          <Component2 options={options} />
        )}
        {options.length > 7 && 
          <Component3 options={options} />
        }
      </View>

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

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