DEV Community

Cover image for 🔺 Flat React Doom Pyramid in 1LOC
All In Developer
All In Developer

Posted on Edited on Originally published at all1n.dev

🔺 Flat React Doom Pyramid in 1LOC

Every React app I opened had the same thing at its root: a pyramid of context providers, each one nested inside the last. It works, but it’s hard to read and painful to reorder. It turns out you can flatten it with a single line.


👉 Continue reading on my blog: 🔺 Flat React Doom Pyramid in 1LOC

Top comments (10)

Collapse
 
teetotum profile image
Martin • • Edited

Interesting approach. Never occurred to me to try such an angle.
After careful consideration I would still refrain from using it in any actual project.
The nesting as it appears to the developer when viewing the jsx source code should not -in such an unexpected way- differ from the actual nesting during runtime.
Code should be as easy to understand as possible, as clear as possible, and as un-confusing as possible.
With this approach it could easily happen that someone gets the order wrong. Or thinks the elements are siblings, when they are not.
But kudos for a clever idea and novel approach.
And lastly, the projects I work with do not really require that many providers.

Collapse
 
Sloan, the sloth mascot
Comment deleted
Collapse
 
ygormartins profile image
Ygor Martins •

Agreed! The first approach can be pretty confusing and overwhelming to look at

Collapse
 
all1ndev profile image
All In Developer •

Now I also agree. Looking at first version looks confusing.

Collapse
 
donkarolel profile image
Karolis Juškauskas •

Great idea, however, what if you moved all of the providers to the Pipe component?

Collapse
 
all1ndev profile image
All In Developer •

Can you please elaborate? 🤔 That was the point. You write the code flat, and the Pipe will make them in a pyramid shape automatically

Collapse
 
donkarolel profile image
Karolis Juškauskas •

You could have all of the providers inside of the, say, Pipe component and then you could do just something like this: <Pipe><App/></Pipe>.

Thread Thread
 
all1ndev profile image
All In Developer •

Yeap

Thread Thread
 
donkarolel profile image
Karolis Juškauskas •

Neat.

Collapse
 
gillarohith profile image
Rohith Gilla •

Haha good one