question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Stateless functional components and shouldComponentUpdate

See original GitHub issue

This is probably a question / documentation issue.

var Aquarium = ({species}) => (
  <Tank>
    {getFish(species)}
  </Tank>
);

In some places of the doc we can read:

https://facebook.github.io/react/docs/reusable-components.html

In an ideal world, most of your components would be stateless functions because these stateless components can follow a faster code path within the React core. This is the recommended pattern, when possible.

https://facebook.github.io/react/blog/2015/10/07/react-v0.14.html

This pattern is designed to encourage the creation of these simple components that should comprise large portions of your apps. In the future, we’ll also be able to make performance optimizations specific to these components by avoiding unnecessary checks and memory allocations.

What I find unclear is these explainations is how React optimize the rendering when using stateless functional components. The good sense would be that React uses something similar to shallowEqual to know if it has to call the function or not, but as React does not enforce strict immutability yet (I mean the PureRenderMixin is actually an option, not the default), I wonder how these functional components behave.

I think this should be better documented if any memoization technique is used when rendering these functional components, because it’s not so obvious to me if my app will perform almost the same (or better) if I choose to replace all my components using PureRenderMixin and no state/lifecycle methods by functional components as I don’t have much insights of the internal working of the optimizations done.

Issue Analytics

  • State:closed
  • Created 8 years ago
  • Reactions:81
  • Comments:42 (11 by maintainers)

github_iconTop GitHub Comments

75reactions
tjconceptcommented, Jan 8, 2016

Interesting read. I assumed functional components would be “pure render” by default when seeing the syntax and reading the blog post about 0.14.

41reactions
Huxprocommented, May 19, 2021

It’s surprising that functional components are not PureComponent by default…

Read more comments on GitHub >

github_iconTop Results From Across the Web

reactjs - shouldComponentUpdate in function components
In React, functional components are stateless and they do not have lifecycle methods. Stateless components are an elegant way of writing ...
Read more >
React shouldComponentUpdate example with functional ...
Hi, If we would like to use functional components instead of classes in this example, then how we should replace the 'shouldComponentUpdate()' ...
Read more >
Improving Performance in React Functional Components ...
The shouldComponentUpdate method is the lifecycle method that React calls when it wants to render a component, this method gives it the go-ahead ......
Read more >
How to prevent re-renders on React functional components ...
The answer is yes! Use React.memo() to prevent re-rendering on React function components. First, if you' ...
Read more >
React useEffect : A hook to introduce lifecycle methods in ...
shouldComponentUpdate () is used if a component's output is affected by the current change in state or props. The default behaviour is to...
Read more >

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found