简体   繁体   English

React 新手,在 Modals 上苦苦挣扎。 这是处理模态的正确方法吗?

[英]New to React, struggling with Modals. Is this the right way to deal with Modals?

I just can't get this to work.我只是无法让它工作。 Although I'm close, I feel like I'm handling Modals in the wrong way.虽然我很接近,但我觉得我以错误的方式处理模态。 Am I?我是吗? I just feel like I'm doing this in a very messy way Lol..我只是觉得我这样做的方式非常混乱,哈哈。

So I have an Events Component..所以我有一个事件组件..

export default function Events(props) {

    const [events, setEvents] = useState([]);
    const [openModal, setOpenModal] = useState(false);

    const newEvent = () =>{
        setOpenModal({openModal:true})
        setEvents(
            events => [...events,
            <Event openModal={true}/>]
        );
    }

    console.log('hi from eventss');
    return (
        <div className="Events" onClick={newEvent} style={{width:'100%'}}>
            {events}
        </div>
    )
}

Events Div...活动分... 活动部

Which returns a div with an 'onclick' event .它返回一个带有'onclick' 事件的 div。 When clicked, it triggers the 'newEvent' function .单击时,它会触发“newEvent” function

The 'newEvent' function creates an Event Component sending a prop openModal=true and sets its state.. 'newEvent' function 创建一个发送道具openModal=true事件组件并设置其 state..

This is the Event Component这是事件组件

const Event = (props) => {

    const [openModal, setOpenModal] = useState(props.openModal);

    return (

        openModal?
        <ModalEvent isOpen={openModal} backgroundcolor = {props.backgroundcolor}/>
        :
        <div className="Event" style={{backgroundColor: props.backgroundcolor}}>
            new event
        </div>
         
    );
}

export default Event;

Event is going to return the ModalEvent Component since openModal=true.. Event 将返回ModalEvent 组件,因为 openModal=true..

This is the ModalEvent component这是ModalEvent 组件

import React, {useState, useEffect} from 'react';
import Modal from 'react-bootstrap/Modal'
import Button from 'react-bootstrap/Button'
import Event from './Event'

function ModalEvent(props) {
    const [openModal, setOpenModal] = useState(true);
    const [address, setAddress] = useState("");

    const handleChange = (e) =>{
      setAddress({address: e.target.value})
    }

    console.log(openModal);

    useEffect(() =>{
      console.log('effectssss');
    })

    return (
      
      openModal?
      <Modal
        size="sm"
        show={openModal}
        onHide={() => setOpenModal(false)}
        aria-labelledby="example-modal-sizes-title-sm"
      >
        <Modal.Header closeButton>
          <Modal.Title id="example-modal-sizes-title-sm">
            Small Modal
          </Modal.Title>
        </Modal.Header>
        <Modal.Body>
          <div className="container">
            <div className="row">
              <form style={{width:'100%'}}>
                <div className="col-sm-12">
                  <label htmlFor="address"> Address</label>
                  <input type="text" name="address" style={{width:'100%'}}/>
                </div>
                {/* <div className="col-sm-12" style={{marginTop:'20px'}}>
                  <input type="submit" value="Submit" />
                </div> */}
              </form>
            </div>
          </div>
        </Modal.Body>
        <Modal.Footer>
          <Button variant="secondary" onMouseDown={() => setOpenModal(false)}>
            Close
          </Button>
          <Button variant="primary" onMouseDown={() => setOpenModal(false)}>
            Save Changes
          </Button>
        </Modal.Footer>
      </Modal>
      :
      <>
      <Event openModal={false} backgroundcolor={'#' + Math.floor(Math.random()*16777215).toString(16)} address={address}/>
      </>

    );
  }
  
export default ModalEvent;

Modal in action...模态在行动...

在此处输入图像描述

The ModalComponent is going to keep returning a Model Component until it sets its openModal=false (which it does through the buttons onMouseDown events). ModalComponent将继续返回Model 组件,直到它设置其 openModal=false (它通过按钮 onMouseDown 事件完成)。

So the PROBLEM is when I click on the modal or anywhere in the screen.. the **Events Component"" is trigger again, which triggers another Modal to pop up..所以问题是当我单击模态或屏幕上的任何位置时.. **Events 组件“”再次触发,这会触发另一个模态弹出..

Like this.....像这样.....

在此处输入图像描述

If I click on the buttons to close the modal, the modal closes how it's supposed to be...如果我单击按钮关闭模式,模式会关闭它应该是的......

If I add an onClick event on the ModalBody/ModalFooter, it stops it from triggering the Event Component but then it closes (of course)... which it doesnt help because the user needs to be able to click inside the model and also input the address..如果我在 ModalBody/ModalFooter 上添加 onClick 事件,它会阻止它触发事件组件,但随后它会关闭(当然)......这无济于事,因为用户需要能够在 model 内部单击并输入地址..

So yea, that it, I'm not really sure how to handle this, I'm not sure if I'm handling Modals in the right way of if I'm missing an idea of how react works... any help is appreciated thank you!所以是的,我不确定如何处理这个问题,我不确定我是否以正确的方式处理模态,如果我错过了反应如何工作的想法......任何帮助都是感谢感谢!

You could look into trying something along the lines of this你可以考虑尝试一些类似的东西

const newEvent = () =>{
        setOpenModal(!openModal)
        setEvents(
            events => [...events,
            <Event openModal={!openModal}/>]
        );
    }

This sets the openModal to the opposite of its boolean value.这会将 openModal 设置为与其 boolean 值相反的值。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM