Generic react bootstrap modal using React ref

Viewed 3704

I'm using a react-table library and when a user clicks on an expand icon I want it to open the table in a modal using (react-bootstrap modal). I need the modal to be generic and each time will get a different graph component or even just regular content. I'm wondering if this is the best practice to implement this feature. I know that it's not recommended to use React ref and according to react docs a dialog is not a good place to use this strategy. But I can't think of other options which will let me pass dynamically the graph component or any other component. Would love to hear other options.

This is my modal component:

const SiteModal = React.forwardRef((props, ref) => {
  return (
    <Modal {...props} size="lg" aria-labelledby="contained-modal-title-vcenter" centered ref={ref} 
            dialogClassName={classes.modalContainer}>
             <Modal.Header closeButton>
              <Modal.Title id="contained-modal-title-vcenter">
                Modal heading
              </Modal.Title>
            </Modal.Header>
           <Modal.Body>
            <h4>{props.children}</h4>
           </Modal.Body>
      </Modal>
     );
   });

This is my parent component which is also a ref element:

const CollapseEelemet = React.forwardRef((props, ref) => {
   const [open, setOpen] = useState(true);
   const [modalShow, setModalShow] = useState(false);

  return (
    <div className={classes.collapseWrapper}>
     <div onClick={() => setOpen(!open)} className={classes.collapseTitle} aria-controls={props.id}
         aria-expanded={open}>
        {props.title}
        <span onClick={() => setModalShow(true)} className="icon-launch"></span>
        **<SiteModal show={modalShow} onHide={() => setModalShow(false)}>
          {props.children}
        </SiteModal>**
     </div>
     <Collapse in={true}>
       <div id={props.id} ref={ref}>
         {props.children}
       </div>
     </Collapse>
   </div>
  );
});

And this is my top parent:

const Cost = (props) => {
   const renderMainGraph = () => {
       const ref = React.createRef();
       return (
          <div className={classes.mainChartWidget}>
           <CollapseEelemet ref={ref} id={constant.mainGraph.id} title={constant.mainGraph.title}>
              <LineChartFusion />
              <GroupColumnChart />
           </CollapseEelemet >
          </div>
         );
     };
 return (
   <div>{renderMainGraph ()}</div>
 );
}
2 Answers

You don't use ref right, React.createRef should be used only in Class components.

const Cost = (props) => {
  const ref = useRef();
  return (
    <div className={classes.mainChartWidget}>
      <CollapseEelemet
        ref={ref}
        id={constant.mainGraph.id}
        title={constant.mainGraph.title}
      >
        <LineChartFusion />
        <GroupColumnChart />
      </CollapseEelemet>
    </div>
  );
}

Ok, so I obviously used the Ref the wrong way. I don't really need to use it at all (FYI Ref are for Class components ONLY!). I deleted all the Refs and ForwordRef and just passed arbitrary children to the component.

This is how is looks now:

Modal Component:

const SiteModal = (props) => {
  return (
    <Modal {...props} size="lg" aria-labelledby="contained-modal-title-vcenter" 
        centered ref={ref} dialogClassName={classes.modalContainer}>
         <Modal.Header closeButton>
          <Modal.Title id="contained-modal-title-vcenter">
            Modal heading
          </Modal.Title>
        </Modal.Header>
       <Modal.Body>
        <h4>{props.children}</h4>
       </Modal.Body>
  </Modal>
});

Expand Component:

 const CollapseEelemet = (props, ref) => {
    const [open, setOpen] = useState(true);
    const [modalShow, setModalShow] = useState(false);

return (
  <div className={classes.collapseWrapper}>
   <div onClick={() => setOpen(!open)} className={classes.collapseTitle} aria- 
    controls={props.id} aria-expanded={open}>
       {props.title}
      <span onClick={() => setModalShow(true)} className="icon-launch"></span>
       **<SiteModal show={modalShow} onHide={() => setModalShow(false)}>
           {props.children}
         </SiteModal>**
       </div>
        <Collapse in={true}>
         <div id={props.id}>
           {props.children}
         </div>
       </Collapse>
     </div>
   });

Top Parent Component:

    const Cost = (props) => {
       const renderMainGraph = () => {
           return (
              <div className={classes.mainChartWidget}>
                <CollapseEelemet id={constant.mainGraph.id} title 
                  {constant.mainGraph.title}>
                    <LineChartFusion />
                    <GroupColumnChart />
                </CollapseEelemet >
              </div>
           );
        };
   return (
       <div>{renderMainGraph ()}</div>
        );
      }
Related