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

110
Vistas
Create a component that repeats a passed component onClick or onSubmit

I have a component which renders "react-icons" object. However, in some situation I only want to pass text rather than an icon to this component.

The component A is as such.

Icon.js, component A

import React from 'react';
import { FaHeart} from "react-icons/fa";

const Icon = ({FaIcon = Faheart}) => {
  return (
    <div>
    {typeof FaIcon === 'string' ? FaIcon : <FaIcon/>}
    </div>
    );
};

export default Icon;

I want create another component B, which will create a container and repeats the component A if clicked. I do not want to put the onClick handle in component A, but component B.

RepeatsonClick.js, component B

This might look like weird code, but something I tried to implement. But, if there something short and elegant that should be fine. But, I am looking for functional based solution.

import React from 'react';
import Icon from './components/icons/icon';

const RepeatsOnClick extends React.Component {

  const [isExit, setIsExit] = useState(false);
  const [count, setCount] = useState([1]);

  const addNewIcon = () => {
    if (!isExit) {
      const lastCount = count[count.length - 1] + 1;
      setCount([...count, lastCount]);
    }
  };

    handleClick(event) {
      event.preventDefault();
      this.setState({
          values: {
              ...this.state.values,
              [Object.keys(this.state.values).length]: '' 
          }
      });
  }
    
    return(
    <div className="container" onClick= handleClick>
      <div>
        {count.map((key) => (
          <Icon FaIcon/>
        ))}
        
        // OR 

        <Icon FaIcon key={key} addNewTextArea={addNewTextArea} />

      </div>
      <button onClick={() => setIsExit(!isExit)}>
        {isExit ? "Restart" : "Stop"}
      </button>
    </div>
      )      
    }   
}
export default RepeatsOnClick;

The Icon component return "Heart Icon" i.e ♥️.

When component B i.e RepeatsOnClick wraps it, I want it to generate another heart icon and so on until it is stopped. ♥️♥️♥️ ...

What I have tried with component B is not working.

How do we resolve this?

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