devmeetings-react-ts-fork
  • Devmeeting React + Typescript
  • Speed dating
  • Typescript
    • It's just JavaScript
    • Basic types
    • Typing objects
    • Typing functions
    • Modules
    • Generics
    • Tasks
  • Hello React
    • Quick look around
    • The first component
    • JSX
    • Tasks
  • Components & Props
    • Small components
    • Passing data to a component
    • Loops in JSX
    • Tasks
  • Stateful components
    • Handling events
    • Components with internal state
    • Talking to a parent component
    • Smart and dumb components
    • Before the break
    • Tasks
  • Side-effects and context
    • Side-effects
    • Prop drilling
    • Using context
    • Tasks
  • What's next
    • Styling
    • Learning component's lifecycle
    • State Management
Powered by GitBook
On this page
  • Standard example
  • When to create a new component?
  • Resources

Was this helpful?

  1. Components & Props

Small components

PreviousComponents & PropsNextPassing data to a component

Last updated 5 years ago

Was this helpful?

The first rule of all best practices in programming is keep it simple. The shorter the code is, generally, the easier it is to understand and reuse. This holds true in React and having two small components that do one thing, and one thing only, is usually way better than having a larger component that needs to handle both of those responsibilities.

Without experience in React or similar libraries/frameworks we usually underestimate just how small the components should be.

Standard example

The images below are . Here's a simple app, much like the one we'll develop:

And here's how guys at Facebook would split it into components:

It this too much? Probably. Way to much? Definitely not!

When to create a new component?

There's no arbitrary number of lines that make the component too big. It comes down to intuition and experience, but try to ask the following questions:

  • what does the component do? If your answer is something along the lines of it does A and B, you probably should split it into two components that do A and B.

  • can I use this code somewhere else? If the answer is yes, then consider extracting the reusable piece into separate component.

  • is this going to be hard to test? Again, if you've answered yes, split into simpler components.

And when in doubt... create a new component!

Resources

Thinking in React
Facebook has 30000 components
shamelessly stolen from React docs
simple app mock
simple app components