I have a codepen on progress that uses bootstrap although I didn't style my modal with bootstrap ... I'm actually using it in the rest of the project here it is: https://codepen.io/manAbl/pen/eKYVpL?editors=1010
I'm creating the instance of the modal with my: <div id="modal-root"></div>
Them I'm creating a portal that stays connected with the hole react state tree with my class Modal
like-so:
class Modal extends React.Component {
constructor(props) {
super(props);
this.el = document.createElement('div');
}
componentDidMount() {
modalRoot.appendChild(this.el);
}
componentWillUnmount() {
modalRoot.removeChild(this.el);
}
render() {
return ReactDOM.createPortal(
this.props.children,
this.el,
);
}
}
and rendering the modal based on state:
class App extends React.Component {
constructor(props) {
super(props);
this.state = {
showModal: false,
};
this.handleModalVisibility = this.handleModalVisibility.bind(this);
}
handleModalVisibility() {
this.setState({ showModal: !this.state.showModal });
}
render() {
const { showModal } = this.state;
return (
<div className="container app-wrapper">
<ListContainer/>
<Button onClick={this.handleModalVisibility} title='Add Recipe' />
{ this.state.showModal ? (
<Modal>
<InputAddRecipe onClick={this.handleModalVisibility} />
</Modal> ) : null }
</div>
)
}
}
Hope helps :)