Hi I want to call reactstrap modal object programatically so that I can reuse it and call it depending on what the situation is. Currently I have it working by using props buy passing the modal state as a prop but I want to not have to manage the modal variable on the parent to make sure my code is a bit cleaner. Let me explain what I want to do:
Using the basic modal example in reactstrap I am removing the button since I will not be calling it when a button is clicked and want to create the modal object on the fly. This is what I have in mind...
/* Default simple reactstrap modal without button */
/* eslint react/no-multi-comp: 0, react/prop-types: 0 */
import React, { useState } from 'react';
import { Button, Modal, ModalHeader, ModalBody, ModalFooter } from 'reactstrap';
const ModalExample = (props) => {
const {
buttonLabel,
className
} = props;
const [modal, setModal] = useState(false);
const toggle = () => setModal(!modal);
const onModalLoad = () => { setModal(true); }
return (
<div>
<Modal isOpen={modal} toggle={toggle} className={className} onOpened={onlLoad}>
<ModalHeader toggle={toggle}>{props.title}</ModalHeader>
<ModalBody>
{props.body}
</ModalBody>
<ModalFooter>
<Button color="secondary" onClick={toggle}>OK</Button>
</ModalFooter>
</Modal>
</div>
);
}
export default ModalExample;
Then I want to create the object on the fly by calling something like:
const parentComponent = () => {
const submitForm = event => {
if (condition1) {
React.createElement( ModalExample, {title: 'issue1', 'this is the problem'} );
}else if (condition2) {
React.createElement( ModalExample, {title: 'issue2', 'this is the problem'} );
}else if (condition3) {
React.createElement( ModalExample, {title: 'issue3', 'this is the problem'} ); }
}
}
return (
<span >
<Form onSubmit={submitForm}>
<Button color="primary" type="submit">
Submit
</Button>
</Form>
</span>
)
}
This is somewhat similar to what is talked about on the link How to open/close react-bootstrap modal programmatically? but this option is not quite talked about and it is not working for me. Not sure what I am missing.