简体   繁体   English

Reactjs中基于多个条件渲染组件

[英]Rendering components based on multiple conditions in Reactjs

I need to improve the rendering logic so as avoid confusing multiple ternary operators.我需要改进渲染逻辑以避免混淆多个三元运算符。 What could be the best way to do this.什么是最好的方法来做到这一点。 I was looking for a way to use the HOC pattern for conditional render here.我在这里寻找一种使用 HOC 模式进行条件渲染的方法。 Kindly provide suggestions.请提供建议。

{

firstCondition ? 
<FirstComponent props1={props1} props2={props2} /> : 
secondCondition || thirdCondition ? 
<SecondComponent props1={props1} props2={props2} /> : 
fourthCondition ? 
<ThirdComponent props1={props1} props2={props2} /> :
<FourthComponent props1={props1} props2={props2} />

} }

let renderContent;

if (firstCondidtion) {
    renderContent =  <FirstComponent />
}
else if (secondCondidtion) {
    renderContent =  <SecondComponent />
}
else{
    renderContent = 
         <>
            <ThirdComponent />
            <FourthComponent />
         </>
    )
}

return renderContent

or或者

return (
     {firstCondition && <FirstComponent />}
     {secondCondition && <secondComponent />}
     {thirdCondition && <><ThirdComponent /><FourthComponent /></>}
)

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

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