9

I'm using react-semantic-ui Modal object. The object that opens the modal is a prop.

 <Modal
       trigger=<Button>Text</Button>
       otherProp=...
   >
  </Modal>

I want to embed Modal in another component:

export default class Confirm extends Component {

    render() {
        return (
            <Modal
                trigger={this.props.trigger} /* here */
              >    
                <Modal.Content>
                    ...
                </Modal.Content>
                <Modal.Actions>
                    ...
                </Modal.Actions>
            </Modal>
        )
    }
}

How can I pass JSX code ( <Button>Text</Button> ) as a prop to be render as a Modal prop?

1

1 Answer 1

21

You can easily do following

<Modal trigger={ <Button>Text</Button> }> 
   // content goes here 
</Modal>

and inside Modal, you can access it via this.props.trigger which will be your button component and you can render it like below

render () {
    return (
        <div>
            // some code can go here
            { this.props.trigger }
        </div>
    );
}
3
  • 1
    Thanks. My question is how can I render this.props.trigger in the Modal tag definition. I want to pass <Button>Text</Button> as a prop
    – znat
    Apr 11, 2017 at 11:52
  • Hi @znat, I have answered already ;), you can print it in Modal like any other string prop or variable. { this.props.trigger } in your render method Apr 11, 2017 at 14:04
  • 1
    I think there's a misunderstanding: I need something like that: <Modal trigger={this.props.trigger}> // content goes here </Modal> But this doesn't work
    – znat
    Apr 11, 2017 at 15:03

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.