⚛️🌲 React Holiday

We'll start from basics and get all the way to Suspense, Hooks, and advanced composition!
Each day is a 2-3 minute read with CodeSandbox and quick assignments.
Check out 2017 (below) for examples.

Day 18: Conscious uncoupling

December 18, 2017

Gweneth got it right. Conscious Uncoupling is a good (for components).

Let’s explore a new smooshing technique that makes PokemonPager reusable.

Insert component here

You can insert components into other components via props.

Because components are functions and JavaScript functions transportable, we can pass components around.

<SomeComponent component={OtherComponent} />

The above might be implemented something like this:

const OtherComponent = () => <h2>👋</h2>

const SomeComponent = props => <props.component />

(Yes, JSX lets you do that)

Uncoupling PokemonPager

Our PokemonPager doesn’t need to be Pokemon specific. With a little work, it could render anything that takes an id prop.

Let’s uncouple it by inserting Pokemon via a component prop.

  <PokemonPager component={Pokemon} />,

Now update the PokemonPager to render this.props.component where <Pokemon /> is.

<this.props.component id={this.state.id} />

Finally, rename PokemonPager to a IdPager, wherever you see it.

Smooshing is magic

We’re getting to the good stuff. Smooshing is the magic of React 🧙‍♂️

This is the first of three smooshing patterns we’ll explore this week.

Tinker with it

Use the workspace below and explore component insertion smooshing.

  • Create a new component called ShowId and insert it into IdPager where Pokemon is now.
  • Read the doc on defaultProps. Use your new ShowId component as the defaultProps for omponent.


Written by chantastic.
Follow his nonsense on Twitter.