简体   繁体   English

模态在子组件中,按钮在父组件中如何关闭 reactjs 中的模态

[英]Modal is in child component where the button is in parent component how to close the modal in reactjs

I have tried some of the methods, it could able to solve my solutions these are example: this is the Parent Component:我尝试了一些方法,它可以解决我的解决方案,例如:这是父组件:

const[openup,setOpenup]=useState(false)

const setSchedule =() => setOpenup(true)

<Button onClick={setSchedule} >Openup</Button>
   <Child open={openup} />

this is the Chile Component:这是智利部分:

export default function CreateSchedule(props) {

 const [show, setShow] = useState(props.open);

 const handleClose =() => setShow(false)
return(
  <div>
     <Modal className='dummy' show={show} onHide={handleClose}>
      Its not working  working !!! 
     </Modal>
  </div>
  ) 
}

Could any one get rid of this one, ...有没有人能摆脱这个,...

Assuming Child is the CreateSchedule commponent, you can redesign your code structure so that the parent is controlling thee rendering of Modal by passing in props like open and also the function to close the modal假设 Child 是 CreateSchedule 组件,您可以重新设计代码结构,以便父级通过传递诸如 open 和 function 之类的道具来控制模态的渲染以关闭模态

Your parent component will look like你的父组件看起来像

const[openup,setOpenup]=useState(false)

const setSchedule =() => setOpenup(true)

const handleClose = () => setOpenup(false);
<Button onClick={setSchedule} >Openup</Button>

<Child open={openup} handleClose={handleClose}/>

Your Child component will be您的子组件将是

export default function CreateSchedule(props) {
    return(
      <div>
         <Modal className='dummy' show={props.open} onHide={props.handleClose}>
          Its not working  working !!! 
         </Modal>
      </div>
      ) 

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 React:如何关闭从父组件打开的子组件的模态 - React: How to close a modal from child opened from parent component 在Reactjs中打开子模式时如何关闭父模式? - how close parent modal when open child modal in Reactjs? 无法使用父 class 组件和子功能组件打开/关闭模态 - Unable to open/close modal with parent class component and child functional component 从父组件关闭模态 - Close modal from the parent component 如何根据从子组件单击的按钮触发父组件中的模态? - How to trigger a modal in the parent component based on the a button clicked from child component? ReactJS 模态组件不会关闭 onclick - ReactJS Modal component won't close onclick 如何使用 ReactJs 中的键从父级打开子组件模态 - How to open child component modal from parent by using its key in ReactJs ReactJS Skylight Modal-在子组件上单击按钮时隐藏模式 - ReactJS Skylight Modal - Hiding modal upon click on button from child component 如何从父组件打开或关闭reactstrap模态 - How to make reactstrap modal open or close from parent component 有 Button onClick function Parent Component 触发 modal 组件时在 Child Component 中出现 - Have Button onClick function Parent Component when it triggers the modal component is in Child Component is to appear
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM