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

233
Visualizações
Avoiding code duplication in react functional components

I have n React functional components which share 90% of their content, but differ in a few functions they use.

Let's say my components are the following:

export default function BWPage(props) {
    const f = (x) => x+1
    const g = (x) => f(x)*2
    const h = (x) => g(x)*x
    return <div>{h(props.input)}</div>
}

export default function ColorPage(props) {
    const f = (x) => x+1
    const g = (x) => {props.setColor(f(x)); return 4;}
    const h = (x) => g(x)*x
    return <div>{h(props.input)}</div>
}

And so on. In OO programming I would obviously override the required function and be happy, but that isn't possible in React. The official guidelines say to use composition, but I don't see how that works here. Passing all functions that can be different as a prop is impractical as then g would be unable to call f. I also cannot create put all common functions somewhere else and import them, as then h isn't able to call g. What is the proper solution here?

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