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

149
Visualizações
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 Respostas
Responde à pergunta

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 Relatório
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