Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

340
Visualizações
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 Respostas
Responde à pergunta

0

you can check it's prototype, for example:

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

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 Relatório

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda