How to add extra onClick handler along with the user's onClick on the button in react?
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?