Community JavaScript Snippet

The "Component Switch" Pattern I Use Instead of Big Render Trees

Big if/else ladders that pick a component by a tag are unreadable by the third branch. The pattern I use instead is a tiny lookup registry. Three accordions on shipping it.

The "Component Switch" Pattern I Use Instead of Big Render Trees

Big if/else ladders that pick a component by a tag are unreadable by the third branch. The pattern I use instead is a tiny lookup registry. Three accordions on shipping it.

JavaScript
Frontend
3 snippets
react
hooks
design-patterns
diegonguyen

By @diegonguyen

April 9, 2026

·

Updated May 18, 2026

780 views

25

4.5 (8)

Once I started writing this pattern, I stopped writing the if/else version. The registry is the entire surface area: every new view type is one line in the object, and the switch component has no behaviour beyond "look it up, render it, pass the rest of the props". Spreading the props rather than enumerating them is what keeps the switch generic; if a future view needs an extra prop, you just add it at the call site. The null return on an unknown tag is a deliberate choice: silently rendering nothing is the right behaviour during a feature flag rollout where the new tag has not landed yet, and the next accordion handles the strict variant for cases where you want a hard error instead.