Skip to content
Home » Blog » 5 Ideas for Flexible React Architectures

5 Ideas for Flexible React Architectures

  • by

One of my least favourite elements of engineering is having to ship the information that what looks like a small characteristic request can’t be achieved with out vital effort, or perhaps shouldn’t be achieved because of the quantity of regression danger the refactor would introduce. It jogs my memory of the joke a couple of vacationer wandering by way of the countryside:

Tourist: “How do I get to London?”
Local: “Well I wouldn’t start from here…”

With that in thoughts, listed below are 5 concepts for React part structure which may prevent from beginning within the incorrect place.

  1. Avoid heavy use of props for types
  2. Use partial utility to create variants
  3. Components will not be layouts
  4. Compose on the level of use
  5. Render kids each time potential

Ideas for Flexible React Architectures

1. Avoid heavy use of props for types

A standard supply of part complexity is when props are used to imperatively set off visible therapies. Prop values ought to declare the state of the part, and never be used as escape hatches to govern the CSS remedy.

The temptation to succeed in right into a part and manipulate a granular CSS property can usually be “design smell” and point out that the visible design system is both inconsistent or not effectively mapped to the states of the part.

Things to contemplate:

  • Am I defining the state of the part? Or telling it how one can do it?
  • Will this prop worth change over the lifecycle of the part?

2. Use partial utility to create variants

One means to enhance the ergonomics of React parts is to pre-apply identified prop values right into a descriptively named part that maps to a particular use. In the next instance, an underlying <Button /> part exports variants that require much less configuration on the level of use, and produces clear, declarative code.

A powerful design system will usually outline these variants upfront of implementation in a particular view. It’s vital to know beforehand if these types might want to adapt to altering prop values being fed into the part. If so, partial utility is probably going not the perfect strategy.

If the fashion guidelines are advanced, disaggregating the types which are impartial of the lifecycle can cut back the complexity of the part’s states.

Things to contemplate:

  • Will this variant be static over the lifetime of the part? or will it must modified dynamically with prop knowledge?
  • Can the visible states be clearly differentiated to easily render logic?

3. Component will not be layouts

Another means that parts fail is that if sizing constraints, or spacing relative to different parts, are laborious coded into the part itself. Besides the headache of managing collapsing margins, core parts usually find yourself included in different parts the place exterior margins may cause issues.

Explicit widths not often carry out effectively in responsive layouts, and might trigger points if the part must be nested inside one other part with a width limitation. If an express width is required, both present a one-off width on the level of use, or be certain that the part is top-level and unlikely to ever be contained in one other part.

Just like the answer to managing state in React is to lift it up from the part, lifting format constraints away from a core part will make it less complicated and extra maintainable as effectively.


  • Don’t embody exterior margin on the part
  • Always count on a part to increase to 100% the width of its container
  • Create one-off containers on the level of use for distinctive constraints

4. Compose on the level of use

Often it’s tempting to optimize for the terseness of code on the level of use as an alternative of for flexibility in part composition. Consider the next:


Really clear on the level of use, however what if I need to management between two totally different card sorts?

And then what if I need to filter the playing cards based mostly on the state of another part on the web page?

You can already see that I’m beginning to go loads of info round. The web page calls the cardboard checklist and tells it what sort of card to render, and it additionally offers a filter perform that, whereas versatile, begins so as to add complexity contained in the part.

Now, what if I’ve a brand new view the place I need to render <CardC />? I’d have to succeed in into the part and add new conditional logic to render the correct card variant. As the logic right here turns into non-trivial, the code turns into more durable to comply with and introduces regression danger to each occasion of the part each time it’s touched. Luckily there’s a straightforward answer.

I’ve now made the code on the level of use extra verbose, however I’ve considerably diminished the general complexity within the system. My <CardList /> has abstracted away right into a extra generalized part to <a href=”management the format of the playing cards, I’ve gained the flexibleness to make use of this format with any subcomponent I want, and I’ve decoupled rendering the info from performing operations on the info. And this brings me to my final level.

5. Use “children” each time potential

The final rule is likely one of the most vital. Whenever potential, goal to permit flexibility in what a part renders. Even trivial assumptions can get baked into the code if you’re not wanting.

What occurs if we need to add an icon together with the textual content? We have to change the bottom part:

But now what if the icon is within the trailing place? We want to change the part once more, and determine the place the <Icon /> ought to go. Using kids solves all of those issues.

And if you happen to ever really feel the necessity, it’s a lot simpler to lock down a part utilizing the partial utility technique described above than it’s to open it up.

Things to contemplate:

  • Is your part solely passing alongside info? or altering it?
  • How a lot variation are you able to count on within the rendered markup?
  • Can I “lock down” a versatile implementation to resolve my quick want with out constraining all future makes use of?


Much like “no plan of battle survives contact with the enemy”, it’s uncommon for even fastidiously thought out design specs to anticipate all future use instances. Thinking upfront about how a part may simply accommodate totally different design wants will assist maintain your implementations from being too opinionated, and prevent time down the highway.

Note: these examples use Styled Components for illustration, however these approaches must be agnostic to the CSS-in-JS answer you employ.

Leave a Reply

Your email address will not be published. Required fields are marked *