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

144
Visualizações
What's the best way to design React component architecture regarding handlers?

So a coworker and I were discussing the best way to write React components, taking into account: readability, developing time, performance.

Suppose we have a medium size component: i.e: 400 lines. I proposed doing something like this:

Folder structure

MyComponent
├── MyComponent.js
├── MyComponentView.js
├── myComponent.scss
├── SubComponent1.js
├── SubComponent2.js
├── utils.js

MyComponent.js

const MyComponent = ({ something }) => {
  const [state, setState] = useState({ something.data });

  const doSomethingHandler = () => { /* here would be some code */ }

  const focusSendButtonHandler = () => { /* here would be some code */ }

  const blurSendButtonHandler = () => { /* here would be some code */ }

  const applyAllButtonHandler = () => { /* here would be some code */ }

  const saveDataHandler = () => { /* here would be some code */ }

  return <MyComponentView {...{ someProps }} />
}

Some handlers use functions that are defined in the utils folder, that I deemed appropriate to extract because they may be used for another thing later.

SubComponent1 and SubComponent2 are dumb components that only render stuff.

Now, his idea was to also move all the handlers to the utils file, or at least outside from the component, in case there is some code that can be reused.

In my opinion, there's no code that can be reused in the handlers because it's all really specific to the component.

Most of the handlers change the state of the component.


So here are my questions:

  1. Is it good practice to move the handlers outside the component in case they might be used later in another part of the code?

  2. Is there a performance issue with taking them outside the component?

  3. Is there a benefit to extract the main functionality of a handler to another function, BUT leaving the handler that calls this function inside the component?

  4. Should I wrap the handlers in a useCallback? All of them are being passed down to MyComponentView SubComponent1 or SubComponent2.

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