This is something you do not always need, but I wrote this article for those looking for it.
Sometimes we might have a generic element, a specific...
For further actions, you may consider blocking this person and/or reporting abuse
Great idea, Chris & Olivier!
This becomes even more powerful if you make it an array of
(wrapper | false)[]
, so you can express multiple conditional wrappers in a more readable (less deep) structure:This allows you to use
P.S.: Wrote that on my cellphone, so this is untested.
Sir you are a legend.
Was this you ? 👇
Nope, I used a normal cellphone. I don't think it would work, had I written it on that.
Oh like that idea! 👀
I just realized this could be made even simpler using Array.prototype.reduce:
Definitely thought-provoking. An abstraction like
ConditionalWrapper
component is one way of doing it in this simplified use case. But it may need serious consideration in the real setting. How about refactoring the duplicated part into a separate component e.g.ServiceCardDetails
while keeping the JSX expression?Abstractions are nice but duplication is far superior if the abstraction does not withstand real challenges. Only you as an engineer can tell if such abstraction will continue to deliver value in the long-run.
Yeah kind of depends on the use-case sometimes your scenario might be the better abstraction.
Some times it might be the ConditionalWrapper.
It comes down to how the abstraction takes place.
Aren't we overthinking this, doesn't this works fine? (Not react dev here)
Not sure how my example is complicated to be honest?
Seems pretty straightforward.
My point was that it seemed like an unnecessary abstraction since the "wet" code is not really a bother to use, you understand?
Not that your code was complicated by any means
Yes Even I am thinking the same....what's the need to complicate....even this keeps our code DRY
The only code that really matter is this piece:
So seems pretty simple right?
The other is just examples of the use-case really.
Hope that clarifies some things 🙏
Why it couldn't be as simple as this below ?
This is kind of the extended write down of the function I described.
So if that's easier for you that will work.
Thanks for this really good piece of code 🙏
Question: When you use Conditionalwrapper inside Servicecard, is the closing
</a>
right or should it be</>
?Ah sorry my bad, should have been the fragment closing 👀
Hi @julia
I believe that might be a typo from the author end,
</>
should be used.Hi Chris,
I really enjoyed your article on using ConditionalWrapper for conditional wrapping in React. In my recent blog post, I introduce an advanced approach using higher order components (HOCs). I found this method to be more flexible and customizable, and I think it might be useful for others looking to add additional functionality to their wrapped elements. Thank you for sharing your technique, and I hope my approach might be of interest to your readers as well.
Thanks for that, help me understand though what's the difference between my generic component and your HOC?
It's just terminology, right?
In a sense, the terms "generic component" and "HOC" are just labels that we use to describe different types of components in software development. However, there are some practical differences between these two types of components that go beyond just the terminology.
An HOC is a function that takes a component and returns a new, enhanced component. This allows you to reuse code and abstract common logic across multiple components, and can be a powerful tool for creating reusable abstractions in your code.
On the other hand, a generic component is simply any component that is not an HOC. It could be a presentational component that only handles the rendering of UI elements, or a container component that handles the data management and behavior of your application.
So while the terms "generic component" and "HOC" are just labels, they do reflect some practical differences in how these components are used and what their purpose is.
I created a library that solves these kind of problems: npmjs.com/package/react-jsx-flow
Thats something handy 😃
Awesome! I'll go check it out
I feel this is similar with Mojo::Template.
FYI
github.com/aliakakis/react-templat...
github.com/aliakakis/react-templat...
Looks like a clean implementation. I will try it out later.
You jus't doing something unconventional method, and that metaphor really great! You're Code Freestyler!
Thanks for sharing!
This is the cleanest solution to conditional rendering I've seen so far!! Really clean implementation
That sounds promising! I will test this in some of my projects! Thanks!
Good thinking! Thanks for sharing
Good material, Chris!
Great article.
Thanks man
Nice idea! 🙌
What would happen if your
ServiceCard
component had a state?wrapper={children => <a href={url}>{children}</a>
would cause the children to re-render, which in your example is acceptable but people will useConditionalWrapper
in different cases. Imagine wrapping animated elements, form elements, charts, maps etc.What happens to the code if I want to use a different wrapper component in one
ServiceCard
?My answer to conditional wrapping is always polymorphic components.
This would be your new component.
Then use it as is to use a
div
.Or an anchor element
Or even a router link
Clean code 🤩
But cant we use React.CreateElement() and conditionally select element type? The last argument of the function takes children. Enlighten me if am wrong 🤔
Not sure I follow you mean for TypeScript safety?
Could you sketch the example here?
Whole code with render. Ignore the types in the example as I have used TS. Works in JS too. Working example: stackblitz.com/edit/vitejs-vite-wb...
Ah yes would work, but less reusable I guess when needing it more than once.
Great trick.
The only problem is that the
else
condition outputs invalid HTML. You shouldn't put block elements inside inline.Here is a trick how to achieve similar functionality !
codepen.io/venelinn/pen/qBKBoPo
Ah yeah didn't really pay much attention to what I wrapped for the example purpose.
This helps so much for my current project. Still got a lot of learning to do and I was trying to find a way around code duplication for a my main navigation.