简体   繁体   English

模态没有出现在反应中

[英]Modal doesnt show up in react

I'm new to react thus the question.我是新手,因此对这个问题做出反应。 This is my CustomModal,这是我的 CustomModal,

import React from 'react';
import {useState} from "react";
import {Button, Modal} from "react-bootstrap";

const CustomModal = (props) =>{
    const [show, setShow] = useState(true);
    const handleClose = () => setShow(false);

    return (
        <div>
            <Modal show={show} animation={false}>
                <Modal.Header closeButton>
                    <Modal.Title>Modal heading</Modal.Title>
                </Modal.Header>
                <Modal.Body>{props.message}</Modal.Body>
                <Modal.Footer>
                    <Button variant="secondary" onClick={handleClose}>
                        Close
                    </Button>
                </Modal.Footer>
            </Modal>
        </div>
    )
};
export default CustomModal;

I want to render it inside a class component when user submits a registration form.当用户提交注册表单时,我想在类组件中呈现它。

class Register extends React.Component {
    state = {
        email : '',
        username: '',
        password: '',
        second_password: ''
    };
    handleSubmit = async (event) =>{
        event.preventDefault();

        const emailValidated = await this.validateEmail(this.state.email);
        const usernameValidated = this.validateUsername(this.state.username);
        const passwordValidated = this.validatePassword(this.state.password, this.state.second_password);
        let registrationComplete = false;
        if(emailValidated === true && usernameValidated === true && passwordValidated === true){
            registrationComplete = await this.register(this.state.email, this.state.username, this.state.password);
            console.log(registrationComplete);
            this.showModal("Hello World!"); //This is the function begin called to show the modal.
        }

    };
    validateUsername = (username) =>{
        return true;
    };

    validatePassword = (password, second) =>{
        return true;
    };

    validateEmail = async (email) =>{
        return true;
    };


    //This is the function that should display the modal

    showModal = (message) =>{
        return (<CustomModal message={message}/>);
    };


    register = async (email, username, password) =>{
        return true;
    };
    render() {
        return (
            <div className="register">
                <h1>Register</h1>
                <Form onSubmit={this.handleSubmit}>
                    <Form.Group controlId="formBasicEmail">
                        <Form.Label>Email address</Form.Label>
                        <Form.Control type="email"
                                      placeholder="Enter email"
                                      value={this.state.email}
                                      onChange={event => this.setState({email: event.target.value})}/>
                        <Form.Text className="text-muted">
                            Please make sure you've access to this mail. You'll receive an activation code here.
                        </Form.Text>
                    </Form.Group>

                    <Form.Group controlId="formPlainText">
                        <Form.Label className="form-label">Username</Form.Label>
                        <Form.Control type="text"
                                      placeholder="Username"
                                      value={this.state.username}
                                      onChange={event => this.setState({username: event.target.value})}/>
                        <Form.Text className="text-muted">
                            Please make it atleast 8 characters long.
                        </Form.Text>
                    </Form.Group>

                    <Form.Group controlId="formBasicPassword">
                        <Form.Label>Password</Form.Label>
                        <Form.Control type="password"
                                      placeholder="Password"
                                      value={this.state.password}
                                      onChange={event => this.setState({password: event.target.value})}/>
                        <Form.Text className="text-muted">
                            Please make sure it's atleast 8 characters long and uses a mix of letters and numbers.
                        </Form.Text>
                    </Form.Group>

                    <Form.Group controlId="formBasicPassword">
                        <Form.Label>Retype Password</Form.Label>
                        <Form.Control type="password"
                                      placeholder="Password"
                                      value={this.state.second_password}
                                      onChange={event => this.setState({second_password: event.target.value})}/>
                    </Form.Group>


                    <Button variant="primary" type="submit">
                        Register
                    </Button>
                </Form>
            </div>
        );
    }
}
export default Register;

All the values are correct and the console log shows true but the Modal doesn't display.所有值都正确,控制台日志显示为真,但不显示模态。 Can someone help me with this?有人可以帮我弄这个吗?

There are some issues on your code您的代码存在一些问题

  1. The customModal has to be part of the render of the Register component customModal 必须是 Register 组件渲染的一部分
  2. There should be a state to track the status of the modal from the Register component应该有一个状态来跟踪来自 Register 组件的模态状态
  3. Also the register has to be notified when the Modal closes当 Modal 关闭时,还必须通知寄存器

I have modified your code and working.我已经修改了您的代码并正常工作。 You can find it live here你可以在这里找到它

I prefer attack this problem creating portals here documentation .我更喜欢解决这个问题,在此处创建门户文档

you might to have issues with z-index in the future.您将来可能会遇到 z-index 问题。

Basically, you create an element outside the DOM hierarchy.基本上,您在 DOM 层次结构之外创建一个元素。

Now, your issue is that, you must render modal inside render method and control it, with boolean state "showModal".现在,您的问题是,您必须在渲染方法中渲染模态并使用布尔状态“showModal”来控制它。

I prepare for you an example:我为你准备了一个例子:

example in my GitHub account 我的 GitHub 帐户中的示例

  • git clone ... git克隆...
  • npm install安装
  • npm run start npm 运行开始

preview:预习:

在此处输入图片说明

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

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