React ๐ŸŽ„

๐ŸŽ‰ Now on video! ๐Ÿ“บ
Buy, buy, buy!!!

Day 18: Conscious uncoupling

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={} />

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.
โ† Previous
Next โ†’

Learn other React stuff...