I have red some articles that say a component that extends React's PureComponent class has better performance than pure functional components in recent versions of React. So if this is correct then are there any use case that pure functional components are good options to use? Meanwhile React team has announced that they will optimize functional components. So who is the winner stateless functional components or components that inherited from PureComponent?
- Pure functional components don't have state, pureComponents have state.Train– Train2018-06-15 17:30:53 +00:00Commented Jun 15, 2018 at 17:30
- Yep, also pure functional components has not optimization, it will re-render always when parent component re-renders.That's my concern.Ismayil Niftaliyev– Ismayil Niftaliyev2018-06-15 19:08:47 +00:00Commented Jun 15, 2018 at 19:08
- Yes, however keep in mind that a PFC's are probably better for repeating elements because they don't have a lifecycle or any logic(You're also forcing people to use them like this and says "hey, that should only do one thing"). Go for a simple PC when you use shallow comparisons. In most scenarios you won't be able to tell the difference in performance. But in the rare case you really really need performance go for the PC.Train– Train2018-06-15 20:30:39 +00:00Commented Jun 15, 2018 at 20:30
Add a comment |
1 Answer
Depends on what you're using them for.
Stateless functions are always dirty, hence always re-render. PureComponents do a shallow comparison so may not need to re-render. However they always do a complete shallow comparison, which also may not be what you want.
It boils down to shouldComponentUpdate and whether or not the pre-written one in PureComponent is suitable for the component in question.
(Noting that I don't know what the current... ahem state of functional components is in React; some of this may have already been baked in.)
2 Comments
Ismayil Niftaliyev
Do you think deciding in parent component to re-render children or not is a good idea?
Dave Newton
@IsmayilNiftaliyev Depends. Not really in general, no; I'd consider that to be mixing concerns.