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

238
Visualizações
Why do react components as class must have `render` method, while those as functions don't?

When I write a component as class, it needs the render method. For example:

class Greet extends React.Component {
  render() {
    return <h1>Hello, world!</h1>;
  }
}

As a result, render() is a method of every Greet instance. Meanwhile, if I write the same component as a factory function, the render() method isn't required:

function Greet() {
  return <h1>Hello, world!</h1>;
}

In both cases, to render that component in the browser window, it's needed to run this:

ReactDOM.render({ 
  <Greet />,
  document.getElementById("root")
});

So, how come that when a class is passed as a parameter to ReactDOM.render(), it needs render() to be included, while a function doesn't?

Is it, by any chance, because the class can't directly return something without encapsulating it in a method, so it was decided for that method to have the same name? In that case, how does the ReactDOM.render() work in the each scenario?

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

In React think of a functional component as been the render part of a class. A lot of times, especially when using composition, that's often all a React component needs to do. So this really does cut down on a lot of boiler plate.

But then that leaves a big hole, like how you handle lifecycle / effect methods etc, and that's were Hooks come in.

So then back to your question => needs render() to be included

You are kind of correct, if you mean a class (constructor) can't return something, well it can, but it's not advised to do so. And since in React render can be called multiple times, it certainly wound't make sense for it to be in the constructor anyway.

So in a nutshell, since a functional React component is the render stage, it of course doesn't require a render function. But since a class constructor shouldn't be used for a render function, it will of course need a render method..

about 4 years ago · Juan Pablo Isaza 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