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

146
Vistas
How to add extra onClick handler along with the user's onClick on the button in react?

I've to extend the default onClick functionality on the button. If a user clicks on it the user's onClick should trigger, but I also have to extend it with triggering my own function with the user's onClick.

I've tried two approaches

  1. With HOC
  2. React.cloneElement

I'm not able to figure out which one to choose. Why choose one over the other? Which one is efficient?

Live Demo

  Codesandbox Demo

App.jsx

import "./styles.css";
import Wrap from "./Wrap";
import EnhancedButton from "./Button";

export default function App() {
  function defaultOnClick() {
    console.log("default on click");
  }
  return (
    <div className="App">
      <Wrap>
        <button onClick={defaultOnClick}> count </button>
      </Wrap>

      <EnhancedButton onClick={defaultOnClick}>
        click from enhanced button
      </EnhancedButton>
    </div>
  );
}

Button.jsx

import React from "react";
import HOC from "./HOC";

function Button({ children, onClick }) {
  return <button onClick={onClick}>{children}</button>;
}

export default HOC(Button);

Wrap.jsx

import React from "react";

export default function WrapWithOnclick(props) {
  const { children } = props;

  function customWrapOnclick() {
    console.log("customWrapOnclick");
  }

  return (
    <>
      {React.Children.map(children, (child) => {
        return React.cloneElement(child, {
          onClick: (e) => {
            customWrapOnclick(e);
            child.props.onClick && child.props.onClick(e);
          }
        });
      })}
    </>
  );
}

HOC.jsx

export default function HOC(Component) {
  return function ModifiedComponent(props) {
    const { children, onClick: hocOnClick, ...rest } = props;

    function customHOCOnclick() {
      console.log("customHOCOnclick");
    }
    return (
      <>
        <Component
          onClick={(e) => {
            customHOCOnclick(e);
            hocOnClick && hocOnClick();
          }}
          {...rest}
        >
          {children}
        </Component>
      </>
    );
  };
}
about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

I don't like the cloneElement approach neither the HOC to be honest. What I would do is this:

import React from "react";

function Button({ children, onClick }) {
  const localOnclick = () => {
    //... do stuff ...
    onClick && onClick()
  }
  return <LibraryButton onClick={localOnclick}>{children}</LibraryButton>;
}

export default Button;

It's pretty straight forward and very simple. Does it cover your case?

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