【问题标题】:calling programmatically reactstrap modal for reuse以编程方式调用 reactstrap 模态以进行重用
【发布时间】:2020-06-17 08:06:38
【问题描述】:

您好,我想以编程方式调用 reactstrap 模态对象,以便我可以重用它并根据情况调用它。目前,我通过使用 props buy 将模态状态作为道具传递,但我不想在父级上管理模态变量以确保我的代码更干净。让我解释一下我想做什么:

使用 reactstrap 中的基本模态示例,我正在删除按钮,因为当单击按钮并想要动态创建模态对象时,我不会调用它。 这就是我的想法......

/* 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;

然后我想通过调用类似的东西来动态创建对象:

   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> 
    )
}

这有点类似于链接How to open/close react-bootstrap modal programmatically? 上讨论的内容,但这个选项没有被充分讨论,它对我不起作用。不知道我错过了什么。

【问题讨论】:

  • 您是否能够控制它是否以编程方式显示在父级中?例如:` const parentComponent = () => { const [showModal, setShowModal] = useState(false) return (
    {showModal && }
    ) }; `
  • 是的,但为了做到这一点,我在父组件中定义了可见性设置为 false 的组件,并跟踪标题、正文和可见性变量。然后我将可见性切换为真/假,因为我需要根据我想要显示的内容显示/隐藏和控制文本,但我觉得这不是一个干净的解决方案,因为我正在处理父模式的数据和变量组件。
  • 如果您希望模态框在不同的场景中可重用,假设这可能意味着不同的标题或正文,那么您将不得不在某些时候将这些细节传递给它。即使在您动态创建元素的示例中......您需要在它被调用的地方定义元素,并且您需要传递一些东西。
  • 我同意,这就是为什么我将道具传递为 {title: 'issue1', 'this is the problem'}, {title: 'issue2', 'this is the problem'},和 {title: 'issue3', 'this is the problem'} 将填写标题和正文...

标签: reactjs reactstrap


【解决方案1】:

您是否能够控制它是否以编程方式显示在父级中?例如:

const parentComponent = () => {
  const [showModal, setShowModal] = useState(false)

return (
    <div>
        {showModal && <Modal title='my title text'  body='my title body'}/>}
        <button onClick={setShowModal(!showModal)}></button>
    </div>
      )
};

在您的示例中,您只是更改了标题,因此您可以将其存储在一个变量中...诚然在父级上:

   const parentComponent = () => { 
let title;    
const submitForm = event => {

   if (condition1) title = 'issue1'; 
   else if (condition2) title = 'issue2';    
   else if (condition3) title = 'issue3'; 

  }
return (
{showModal && <Modal title={title}  body='my title body'}/>}
)
}

如果您绝对不想在父项上存储值,您可以在返回中使用完整的条件,但这对我来说似乎很混乱(实际上,这是父项的返回语句,所以您不会将数据从父):

    return (
<div>
{condition1 && <Modal title='issue1'  body='my title body'}/>}
{condition2 && <Modal title='issue2'  body='my title body'}/>}
{condition3 && <Modal title='issue3'  body='my title body'}/>}
)

这假设只有一个条件为真 - 否则您最终可能会得到三个模态!

【讨论】:

  • 不确定我是否遵循,你能建立那个例子并告诉我条件会是什么
  • 我编辑了这个问题,但基本上我不想跟踪父组件中的 showModal 或任何其他变量以使其他组件独立。还要考虑可重用性,如果我想在不同的父组件中使用相同的模态组件,这不是一个很好的方法,或者我认为。我不确定我是否解决了问题,但请随时发表评论。
  • 所以在某些时候你必须让父级知道模态组件。在您的示例中,您正在尝试创建“ModalExample”类型的元素 - 这不是像 div 这样的内置元素,因此除非您告诉您调用它的位置,否则它不会知道。还有很多重复的代码,在每个条件中调用create。我会更新我的答案以显示另一个选项
猜你喜欢
  • 1970-01-01
  • 2019-05-07
  • 2017-12-30
  • 1970-01-01
  • 2020-03-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-12-14
相关资源
最近更新 更多