Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

When to use ES6 class based React components vs. functional ES6 React components?

After spending some time learning React I understand the difference between the two main paradigms of creating components.

My question is when should I use which one and why? What are the benefits/tradeoffs of one over the other?


ES6 classes:

import React, { Component } from 'react';  export class MyComponent extends Component {   render() {     return (       <div></div>     );   } } 

Functional:

const MyComponent = (props) => {     return (       <div></div>     ); } 

I’m thinking functional whenever there is no state to be manipulated by that component, but is that it?

I’m guessing if I use any life cycle methods, it might be best to go with a class based component.

like image 566
omarjmh Avatar asked Mar 19 '16 04:03

omarjmh


People also ask

When would you use class or function component React?

A functional component is just a plain JavaScript pure function that accepts props as an argument and returns a React element(JSX). A class component requires you to extend from React. Component and create a render function which returns a React element. There is no render method used in functional components.

Which is better functional component or class component in React?

Because they're lightweight, writing these simple components as functional components is pretty standard. If your components need more functionality, like keeping state, use classes instead.

Are functional components better than classes?

Functional components show a greater performance than class components. The point used to measure this is the React functional element made of a simple object with the type(string) and props(object) 2 properties. Rendering such a component needs to call the function and passing props.


2 Answers

Old Answer: You have the right idea. Go with functional if your component doesn't do much more than take in some props and render. You can think of these as pure functions because they will always render and behave the same, given the same props. Also, they don't care about lifecycle methods or have their own internal state.

Because they're lightweight, writing these simple components as functional components is pretty standard.

If your components need more functionality, like keeping state, use classes instead.

More info: https://facebook.github.io/react/docs/reusable-components.html#es6-classes


New Answer: Much of the above was true, until the introduction of React Hooks.

  • componentDidUpdate can be replicated with useEffect(fn), where fn is the function to run upon rerendering.

  • componentDidMount methods can be replicated with useEffect(fn, []), where fn is the function to run upon rerendering, and [] is an array of objects for which the component will rerender, if and only if at least one has changed value since the previous render. As there are none, useEffect() runs once, on first mount.

  • state can be replicated with useState(), whose return value can be destructured to a reference of the state and a function that can set the state (i.e., const [state, setState] = useState(initState)). An example might explain this more clearly:

const Counter = () => {   const [count, setCount] = useState(0)    const increment = () => {      setCount(count + 1);   }    return (     <div>       <p>Count: {count}</p>       <button onClick={increment}>+</button>     </div>   ) }  default export Counter 

As a small aside, I have heard a number of people discussing not using functional components for the performance reasons, specifically that

"Event handling functions are redefined per render in functional components"

Whilst true, please consider if your components are really rendering at such a speed or volume that this would be worth concern.

If they are, you can prevent redefining functions using useCallback and useMemo hooks. However, bear in mind that this may make your code (microscopically) worse in performance.

But honestly, I have never heard of redefining functions being a bottleneck in React apps. Premature optimisations are the root of all evil - worry about this when it's a problem

like image 126
Jeff Cousins Avatar answered Oct 21 '22 08:10

Jeff Cousins


UPDATE March 2019

Building on what was stated in my original answer:

"Are there any fundamental differences between React functions and classes at all? Of course, there are — in the mental model."

https://overreacted.io/how-are-function-components-different-from-classes/

UPDATE Feb 2019:

With the introduction of React hooks, it seems as though the React teams wants us to use functional components whenever possible (which better follows JavaScript's functional nature).

Their motivation:

1.) It’s hard to reuse stateful logic between components 2.) Complex components become hard to understand 3.) Classes confuse both people and machines 

A functional component with hooks can do almost everything a class component can do, without any of the draw backs mentions above.

I recommend using them as soon as you are able.

Original Answer

Functional components aren't any more lightweight than class based components, "they perform exactly as classes." - https://github.com/facebook/react/issues/5677#issuecomment-241190513

The above link is a little dated, but React 16.7.0's documentation says that functional and class components:

"are equivalent from React’s point of view." - https://reactjs.org/docs/components-and-props.html#stateless-functions

There is essentially no difference between a functional component and a class component that just implements the render method, other than the syntax.

In the future (quoting the above link) "we [React] might add such optimizations."

If you're trying to boost performance by eliminating unnecessary renders, both approaches provide support. memo for functional components and PureComponent for classes.

-https://reactjs.org/docs/react-api.html#reactmemo

-https://reactjs.org/docs/react-api.html#reactpurecomponent

It's really up to you. If you want less boilerplate, go functional. If you love functional programming and don't like classes, go functional. If you want consistency between all components in your codebase, go with classes. If you're tired of refactoring from functional to class based components when you need something like state, go with classes.

like image 21
Galupuf Avatar answered Oct 21 '22 09:10

Galupuf