Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How many components are too many in ReactJS?

Tags:

reactjs

Take a "Contact Us" form as an example. I could just build the entire thing in one component, or it could be really decomposed: every text field is a component, submit button, etc .... How does one know how much form should be deconstructed and rebuilt as Components?

like image 620
Jason Leach Avatar asked Apr 18 '16 23:04

Jason Leach


People also ask

Can you have too many components React?

Connecting too many components is an issue that should be avoided when working in React and Redux. Having too many components connected to the state can cause things to go wrong with updating the data and how it gets displayed on the screen.

How many components should a React app have?

You'll see here that we have five components in our app. We've italicized the data each component represents. The numbers in the image correspond to the numbers below.

How many lines is too many for a React component?

50 lines is a good rule of thumb for the body of your component (for class components, that is the render method). If looking at the total lines of the file is easier, most component files should not exceed 250 lines. Under 100 is ideal. Keep your components small.

How many props are too many React?

I follow this rule of thumb: Three props is fine. Five props is a code smell. More than seven props is a disaster.

What happens when you mount a React component?

After a React component does mount, it will be listening for any React props or state that has changed. Once it detects something has changed, it will, by default, re-render the entire React component and it’s child components.

How to use React shouldcomponentupdate?

Here’s a basic example of how to use React shouldComponentUpdate. First I’ll start by creating 2 basic React components. In the images above, I’ve created 2 React components. One is a greeting component, and the other is the app component. Each React component is console logging a message during the render lifecycle.

What is the average length of React components?

Anecdotally, I would say that in practice my react components are somewhere between 50-300 lines in length, with the top-level components being the largest and most complex.

What happens when you re-render React components?

After a React component does mount, it will be listening for any React props or state that has changed. Once it detects something has changed, it will, by default, re-render the entire React component and it’s child components. Is it bad to re-render multiple React components?


1 Answers

My rule is to component-ize things that can and will be re-used in your app, either aesthetically or functionally. If this is the only form you need it is probably not worth the overhead of converting each element to a component. But if you will have many forms consider making components.

Example reasons to component-ize your form elements:

  • Special form field functionality (InputEmail: does front-end validation for email inputs)
  • Styling (SubmitButton: always red, full width, etc.)

In bigger projects I have worked on this has worked out well. I have a set of customizable form components so I can build a styled form with complex functionality. Usually it will look something like:

<Form onSubmit={this.handleSubmit}>
    <Input default label="Email Address" type="email" validate={validateEmail} />
    <Input default label="Password" type="password" />
    <Button primary type="submit" />
</Form>
  • Form performs general validation logic
  • Input runs a validateEmail prop and renders the label
  • You get the idea...
like image 186
ebuat3989 Avatar answered Oct 07 '22 00:10

ebuat3989