Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

145
Views
¿Cómo agregar un controlador onClick adicional junto con el onClick del usuario en el botón de reacción?

Tengo que extender la funcionalidad onClick predeterminada en el botón. Si un usuario hace clic en él, el onClick del usuario debería activarse, pero también tengo que extenderlo activando mi propia función con el onClick del usuario.

He intentado dos enfoques

  1. Con HOC
  2. React.clonElement

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

Demo en vivo

Demostración de Codesandbox

Aplicación.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> ); }

Botón.jsx

 import React from "react"; import HOC from "./HOC"; function Button({ children, onClick }) { return <button onClick={onClick}>{children}</button>; } export default HOC(Button);

Envolver.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 answers
Answer question

0

No me gusta el enfoque cloneElement ni el HOC para ser honesto. Yo lo que haría es esto:

 import React from "react"; function Button({ children, onClick }) { const localOnclick = () => { //... do stuff ... onClick && onClick() } return <LibraryButton onClick={localOnclick}>{children}</LibraryButton>; } export default Button;

Es bastante directo y muy simple. ¿Cubre tu caso?

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!