Rule Proposal: always use functional components for pure render component
See original GitHub issueThis code should be always
class Something extends Component {
componentDidMount() {
someSideEffect()
}
render() {
return (
<div>
// Some complex structure
</div>
)
}
}
refactored to this:
const SomethingRenderer = () => (
<div>
// Some complex structure
</div>
)
class Something extends Component {
componentDidMount() {
someSideEffect()
}
render() {
return <SomethingRenderer />
}
}
Proposal
Always extract rendering into separate functional render components. Never mix them with dirty class based components.
Benefits:
- Composability of renderers - because they will have no dependencies on hooks/state/instance.
- Readability of renderers - data comes from props only, not from state/instance
- 0 extra work once you want to reuse renderer somewhere else, just move it to a separate file or export it.
- Render components are likely to survive any react api changes, because they only rely on props, function with props returning jsx will never change.
Issue Analytics
- State:
- Created 6 years ago
- Reactions:1
- Comments:7 (4 by maintainers)
Top Results From Across the Web
What are React pure functional components?
React supports two types of components, class components and functional components. A functional component is a plain JavaScript function that ...
Read more >React functional stateless component, PureComponent ...
Functional components are always re-rendered when the parent component gets updated, even if they don't use props at all. example. – AlexM. Aug ......
Read more >Use React.memo() wisely
memo () is when you expect the functional component to render often and usually with the same props. A common situation that makes...
Read more >Keeping Components Pure
React's rendering process must always be pure. Components should only return their JSX, and not change any objects or variables that existed before...
Read more >New rule proposal: react/prefer-functional-component #2860
Where always enforces the use of functional components everywhere, allow-stateful behaves as the current prefer-stateless-function rule, ...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Certainly it can be. What I’m trying to suggest is that it’s a bad rule because it would be forcing separation that isn’t needed to ensure a clean codebase.
Since there aren’t any others who’ve expressed interest, I’ll close this.