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.

Rule Proposal: always use functional components for pure render component

See original GitHub issue

This 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:

  1. Composability of renderers - because they will have no dependencies on hooks/state/instance.
  2. Readability of renderers - data comes from props only, not from state/instance
  3. 0 extra work once you want to reuse renderer somewhere else, just move it to a separate file or export it.
  4. 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:closed
  • Created 6 years ago
  • Reactions:1
  • Comments:7 (4 by maintainers)

github_iconTop GitHub Comments

3reactions
ljharbcommented, Mar 5, 2018

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.

0reactions
ljharbcommented, Feb 4, 2022

Since there aren’t any others who’ve expressed interest, I’ll close this.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

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