在 React 中构建 Modal 组件而不将其嵌套在调用者组件中 [英] Building a Modal component in React without nesting it inside caller component

查看:28
本文介绍了在 React 中构建 Modal 组件而不将其嵌套在调用者组件中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试从非相关组件调用一个 Modal(不使用任何父子关系).

为了做到这一点,我正在尝试使用 React Redux(这是我见过的可以在两个不相关组件之间建立连接的唯一方法).CodeSandbox 上的示例 显示了我正在尝试做的最起码的事情.

我的问题是我不想在

<style type="text/css">{身体{"+溢出:隐藏"+"}"}</风格>

, 文档.body):<></>}

};导出默认模态;

I'm trying to call a Modal from non-related component (without using any parent-child relationship).

In order to do that I'm trying to use React Redux (as the only way I've seen that can make a connection between two unrelated components). An example on CodeSandbox shows the bare minimum of what I'm trying to do.

My issue is that I don't want to include <Modal> inside the <Button> render function. I want to be able to simply flip the flag in Button.js and <Modal> would appear. This is, from what I understand, is suppose to be one of the advantages of Redux.

It may be look unimportant, but besides the fact that I understand that this is something that can be done and so I want to know how, it will be useful for me in a different piece of code in which if I include <Modal> in the component's render function it'll render the Modal multiple times (I render that component in a list).

Edit:

Just to be clear (as per the example on CodeSandbox), I'm using React classes and not functional components; so no hooks like useDispatch but rather functions like mapDispatchToProps are the way I want to go here.

解决方案

I will Recommend using React Portal, this will inject it inside the given node, I found it to be the best solution for creating modals. I used same in dailylivedeals.com as a POC

import ReactDOM from "react-dom";

render() { 
    return ReactDOM.createPortal(
    this.props.children,
    Document.body
  );
}

This is the simplest and cleanest using React's own feature.

Advantage:

  1. Cleaner and simpler
  2. Each modal instance can have its own modal
  3. Multiple modals can be opened ( even from inside a modal)
  4. Modal target can be dynamic (like modal inside modal)
  5. Multiple modal can be controlled using code easily.

Update :

Eloborate code for modal

import React, {useEffect, useState} from "react";
import ReactDOM from "react-dom";
import {Link} from 'react-router-dom';

import "./modal.scss";

let Modal = ({visible, id, hideModal, children, ...props}) => {

    let [show, setShow] = useState(false);

    useEffect(() => {
        setShow(visible);
        console.log(visible);
    }, [visible]);

    let toggleVisibility = () => {
        //hideModal();
        setShow(!show);
    }

    useEffect(() => {
        if (!show) {
            hideModal();
        }
    }, [show]);


    return <div className="modal-scratchpad">
        {show ?
            ReactDOM.createPortal(
                <div id={`${id}-modal-wrapper`} className="sample-modal-wrapper">
                    <div id={`${id}-modal-backdrop`} className="sample-modal-backdrop">
                    </div>
                    <div id={`${id}-modal-container`} className="sample-modal-container">
                        <div id={`${id}-modal`} className="sample-modal">
                            {children}
                            <div onClick={toggleVisibility} className="sample-modal-cross-button">{'\u2716'}</div>
                        </div>
                        <style type="text/css">
                            {"body {" +
                            "overflow:hidden" +
                            "}"}
                        </style>
                    </div>
                </div>
                , document.body)
            : <></>
        }
    </div>

};

export default Modal;

这篇关于在 React 中构建 Modal 组件而不将其嵌套在调用者组件中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
前端开发最新文章
热门教程
热门工具
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆