DEV Community

Discussion on: Cleaner React: Conditional Rendering

Collapse
 
rtivital profile image
Vitaly Rtishchev
const IfSingleItem = ({ children, data }) => <RenderIf isTrue={data.length === 1}>{children}</RenderIf>  
const IfDoubleItem = ({ children, data }) => <RenderIf isTrue={data.length === 2}>{children}</RenderIf>  
const IfMultiItem = ({ children, data }) => <RenderIf isTrue={data.length > 3}>{children}</RenderIf>

const RenderData = ({ data }) => {  
 return (  
   <>  
     <IfSingleItem data={data}>  
       <SingleItem data={data} />  
     </IfSingleItem>  
     <IfDoubleItem data={data}>  
       <DoubleItem data={data} />  
     </IfDoubleItem>  
     <IfMultiItem data={data}>  
       <MultiItem data={data} />  
     </IfMultiItem>  
   </>  
 );  
}  
Enter fullscreen mode Exit fullscreen mode

This looks really nasty. It's always better to such keep logic out of jsx. We have a switch operator for that.

Collapse
 
sturdynut profile image
Matti Salokangas

I see what you are saying; you could very well use a switch operator as well. The concept here was to demonstrate how you can wrap RenderIf to create a richer API for your JSX.

Collapse
 
rtivital profile image
Vitaly Rtishchev

What I'm saying is that RenderIf is a huge mess and should not be used

Thread Thread
 
sturdynut profile image
Matti Salokangas • Edited

I've used it on several projects where teams were really happy with it. I'm curious why you think it is a "huge" mess considering the alternatives and potential pitfalls?