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

117
Views
Accessing parent method from a component received as a prop

I'm trying to build a reusable Confirmation component that renders a button and when clicked, it should open a Material UI Dialog. The button component gets passed in as a prop for the Confirmation component

<Confirmation component={() => <MUIButton>Click me</MUIButton>} />

The parent component looks like this

const Confirmation = ({ component: Component }) => {

  const handleClick = () => {
    ...logic to open the dialog...
  }

  return (
    <>
      <Component onClick={handleClick} <-- how to trigger this? />
      <Dialog />
    </>
  )
}

Now how would I get this to work without having to specify the onClick in the passed button component itself? For this situation one can assume the component passed as a prop is always some kind of a button.

<Confirmation
  component={() => (
    <MUIButton
      onClick={...logic} <-- don't want to have to specify this
    >
      Click me
    </MUIButton>
  )
/>

OR am I approaching this from a wrong perspective? Should this be solved by passing the button as a child instead? As in

<Confirmation>
  <MUIButton> Click me </MUIButton>
</Confirmation>

and how would the implementation be in this situation?

Thank you in advance!

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

Ended up solving this by creating a higher order component as suggested by John

const withConfirmation = (WrappedComponent) => {
  return (props) => {
    return (
      <>
        <WrappedComponent

          // Overrides the possible onClick logic passed as a prop
          onClick={ ...dialog opening logic }

          {...props}
        />
        <Dialog>
          ...
        </Dialog>
      </>
    );
  };
};
const Confirmation = withConfirmation(MuiButton)

<Confirmation>Clicking me opens a dialog</Confirmation>
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!