Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I give props to a typescript component that is being passed as a props in react?

I have a component 'Modal.tsx' that I want to consume content from another component, similar to the below.

<Modal onClose={() => setShowModal(false)} Content={ConfirmationContent} />

ConfirmationContent lives in its own ConfirmationContent.tsx file which imported in to the the Modal.tsx file.

I can display content from the ConfirmationContent component successfully within Modal.tsx

type Props = {
  showModal : boolean,
  Content: React.ElementType,
}
const Modal = ({showModal, Content} : Props) => { 
  return (
    <div><Content /></div>
  )
}

But what I 'd really like to do is pass props from the parent of Modal.tsx in to ConfirmationContent, kinda like this ..

<Modal onClose={() => setShowModal(false)} Content={ConfirmationContent={text='hotpotato' closeModal={false} } />

But I'm struggling to understand how to do this - Could it be the React.ElementType ?

Any help or advice here would be truly appreciated.

like image 708
Jane Delugas Avatar asked Oct 28 '25 22:10

Jane Delugas


1 Answers

Change content to children this way:

type Props = {
  showModal : boolean,
  children?: React.ReactNode,
}
const Modal = ({showModal, children} : Props) => { 
  return (
    <div>{children}</div>
  )
}

And then you can pass props you'd like this way:

<Modal onClose={() => setShowModal(false)}>
    <ConfirmationContent onClose={() => {//...}} text="foo" >
</Modal>

like image 158
Mathieu Guyot Avatar answered Oct 31 '25 15:10

Mathieu Guyot



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!