Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

188
Vistas
React: class to function component where class extends and has constructor with parameters

How to rewrite when a class component extends from another class? Parameters in the constructor is what makes me unsure how to handle. Anyone with ideas?

Code example.

class Animal extends React.Component {
    constructor(name, action, addKeywords = [], addHashtag = []) {
        super();
        this.name = name;
        this.action= action;
        this.currentUser = new CurrentUser();
        this.keywordList = FILTER_BY_KEYWORDS.concat(addKeywords );
        this.hashtagList = addHashtag;
    }

    nameChange(name) { ... }
}

class Lion extends Animal {
    render() {
        return <Item name={this.nameChange} />
    }
}
about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

While inheritance was never encouraged in class components, sometimes we cannot choose the shape of the legacy code that we want to migrate to Hooks. Each old component will be different and I don't encourage anyone to write following code when working on a new project using React Hooks.

That said, following is the most "direct translation" of that particular class structure from the question into a function component with hooks that I can think of:

function useAnimal(name, action, addKeywords = [], hashtagList = []) {
  const self = React.useRef()
  if (!self.current) {
    // simulate synchronous constructor method, using the initial prop values
    self.current = {
      name,
      action,
      currentUser: new CurrentUser(),
      keywordList: FILTER_BY_KEYWORDS.concat(addKeywords),
      hashtagList,
      nameChange: (name) => { ... } 
    }
  }

  return self.current
}


function Lion({name, action, addKeywords, addHashtag}) {
  const {nameChange} = useAnimal(name, action, addKeywords, addHashtag)

  return <Item name={nameChange} />
}
about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda