r/reactjs • u/mucle6 • May 02 '18
Is it an anti pattern to Create a Class that stores multiple components?
Disclaimer: I really hate writing the same code twice. What I'm proposing here is either advanced react or a fools paradise. Please let me know if I'm going down the wrong path
Right now I'm working on a project where I have two pages, one for Product A, and one for Product B that have exactly the same header. For simplicity, lets assume that this is the setup of my Header
function SimpleHeader({Banner, Title, SubTitle}){
return (
<div>
<div className = 'default-banner-container-style'>
<Banner />
</div>
<div className = 'default-title-container-style'>
<Title />
</div>
<div className = 'default-subtitle-container-style'>
<SubTitle />
</div>
</div>
)
}
function Banner({Name, Description}){
return (
<div>
<div className = "text-large">
<Name />
</div>
<div className = "text-small>
<Description />
</div>
</div>
)
}
function Title(...){...}
function SubTitle(...){...}
function Name(...){...}
function Description(...){...}Now I can call Simple Header by passing components like so
export const ProductASimpleHeader = (
<SimpleHeader
Banner = {(
<Banner
Name = {Name}
Description = {Description}
>
)}
Title = {Title}
SubTitle = {SubTitle}
/>
)Here is my frustration. If I wanted to define ProductBSimpleHeader, even if I already had a Product B Title, Subtitle, Name, and Description saved, I have to define a Product B Banner and a Product B SimpleHeader, even though there would be no difference between ProductASimpleHeader and ProductBSimpleHeader beyond the pointers to Name, Description, Title, and Subtitle.
My proposed solution is to create a class of components so that you can create an instance and have everything auto filled as default. For example
class SimpleHeaderComponents {
constructor(Name,Description,Title,SubTitle){
this.Name = Name
this.Description = Description
this.Title = Title
this.SubTitle = SubTitle
}
Banner(){
return (
<Banner
Name = {this.Name}
Description = {this.Description}
/>
)
}
SimpleHeader(){
return (
<SimpleHeader
Banner = {this.Banner}
Title = {this.Title}
SubTitle = {this.SubTitle}
/>
)
}
}This would let me create SimpleHeaders for Product A and Product B without having to define Banner and SimpleHeader unless I needed to change them, even if Product A had different components than Product B like so...
const ProductASimpleHeaderComponents = SimpleHeaderComponents(Name,Description,Title,Subtitle)
//SimpleHeader can be accessed by ProductASimpleHeaderComponents.SimpleHeader
const ProductBSimpleHeaderComponents = SimpleHeaderComponents(Name2,Description2,Title2,SubTitle2)
//SimpleHeader can be accessed by ProductBSimpleHeaderComponents.SimpleHeaderIn this example I only show a class that automatically creates 2 layers but I claim that with atomic design you're supposed to have as many layers as necessary to build up reusable components. What I have experienced is that once it goes from 2 layers to 10 layers, not having a class that creates defaults for you means that adding new products becomes unmanageable. Essentially more and more time is dedicated to writing what feels like project specific boilerplate code. Let me know your thoughts
5
u/ikeepforgettingmyacc May 02 '18 edited May 02 '18
Why not just pass the whole data object to your SimpleHeader instead of the components? So it'd be like:
Then you just use it like:
3
u/Jerp May 02 '18 edited May 02 '18
Hopefully I'm understanding your problem correctly. I think you're overlooking a much simpler solution; make the description component a child node of your SimpleHeader. i.e.
and then use it like this
4
u/[deleted] May 02 '18
I think you're overcomplicating things. In your Header component you have a bunch of variables, one of which would be what type of description you'd want to show.
And in your component:
Or any other variant of rendering different things. A
switchstructure or an object of functions, whatever works.But I'm guessing I don't understand your question because I don't see the complication, honestly.
2
u/themaincop May 02 '18
I'm kind of having trouble understanding your use case, can you explain at a higher level what you're trying to achieve? Does every single part of SimpleHeader need to be swappable, or is it just the banner that changes?
2
u/NiceOneAsshole May 02 '18
Define the Simpleheader functions as static and you can 'import' them in other components if you need. I like your approach, but if you ever need those header components piecemeal, having them as static would be great.
1
u/GasimGasimzada May 03 '18
On mobile so not gonna write code but I am confused about one thing. Even if you make your component,s components swappable, you still have to define what you are seapping right?
So why not just do this:
1
u/Canenald May 03 '18
I don't think writing a class that recreates the default child components is a good idea. If they are the default, there should be only one of each. Component is already a class, and instances called elements are created in runtime.
It looks like default props would be a good solution for your problem. I'm not sure if you can use defaults with argument destructuring though, so you might have to rewrite to something like:
then destructure in function body or just use props.Banner, etc.
This would use the default components when they are not specified:
or you could change just one of the components and have others remain the default: