Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

338
Views
React - how to determine if component is stateless/functional?

I have functional/stateless component and component which inherited from React.Component:

const Component1 = () => (<span>Hello</span>)

class Component2 extends React.Component {
  render() {
    return (<span>Hello</span>)
  }
}

How can I determine if component is stateless or not? Is there any official way?

isStateless(Component1) // true
isStateless(Component2) // false
over 4 years ago · Santiago Trujillo
3 answers
Answer question

0

you can check it's prototype, for example:

function isStateless(Component) {
    return !Component.prototype.render;
}
over 4 years ago · Santiago Trujillo Report

0

Class components are inherently stateful, but with the introduction of React hooks functional components are no longer called stateless because they can be stateful, too.

isReactComponent special property exists on React.Component since React 0.14. This allows to determine whether a component is class component.

function isFunctionalComponent(Component) {
  return (
    typeof Component === 'function' // can be various things
    && !(
      Component.prototype // native arrows don't have prototypes
      && Component.prototype.isReactComponent // special property
    )
  );
}

function isClassComponent(Component) {
  return !!(
    typeof Component === 'function'
    && Component.prototype
    && Component.prototype.isReactComponent
  );
}

Similar checks are performed in React code base.

Since components can be various things like context Provider or Consumer, isFunctionalComponent(Component) may not be equal to !isClassComponent(Component).

over 4 years ago · Santiago Trujillo Report

0

Depends on what does one call "stateless". If by stateless one means "can't have a ref" then it's:

function isStateless(Component) {
  return typeof Component !== 'string' && !Component.prototype.render
}

Update: there's also a new PropTypes.elementType type for React components (not "elements").

over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!