LaPoule
LaPoule

Reputation: 323

ReactJS functionnal or pure for stateless components?

What is the best between functionnal component and PureComponent when it's a stateless component ?

This :

import React from 'react';

export default function Example(props){
  return (
    <div>
      {props.myProps}
    </div>
  )
}

Or this :

import React from 'react';

export default class Example extends React.PureComponent {

  render() {
    return(
      <div>
        {this.props.myProps}
      </div>
    )
  }
}

Upvotes: 0

Views: 118

Answers (2)

Rohith Murali
Rohith Murali

Reputation: 5669

When its a stateless component, and you are not using shouldComponentUpdate() or any lifecycle methods, then you should go with functional components as this will be light weight than PureComponents.

Upvotes: 1

AjayK
AjayK

Reputation: 443

Functional components are more of building blocks and easy to reason about and can be/should be composed to make more complex components. And as per React docs, they might be able to enhance the performance of Functional components in future.

Pure Component is just a regular component with a syntactic sugar around props check in case one needs to implement shouldComponentUpdate based on props check; with Pure components you get that by default.

Upvotes: 0

Related Questions