react封装弹窗

Index

import { message } from '@/utils/message'

const Index = () =>
{
    const btnClick = () =>
    {
        message('要展示的内容', (close) =>
        {
            console.log('点击了关闭按钮');
            close()
        })
    }
    return (
        <div>
            <h1 >Page index</h1>
            <button onClick={btnClick}>点击</button>
        </div>
    );
}

export default Index

message

import React, { Fragment } from 'react'
import { createRoot } from 'react-dom/client';
const contentStyle = {
    position: 'fixed',
    width: '100%',
    height: '100%',
    left: 0,
    top: 0,
    background: ' rgba(255, 255, 255, 0.8)',
    display: 'flex',
    justifyContent: 'center',
    alignItems: 'center',
    zIndex: '99'
};
const divStyle = {
    background: '#fff!',
    color: '#333',
    padding: '1em 2em',
    borderRadius: '10px',
    boxShadow: '0 0 3px #000',
    display: 'flex',
    flexDirection: 'column',
    alignItems: 'center'
}
const textStyle = {
    marginBottom: '1em'
}
export function message (message, callBack)
{
    const app = document.createElement('div');
    app.id = 'model'
    document.body.appendChild(app);

    const close = () =>
    {
        app.remove() // 删除APP节点
        root.unmount() // 卸载root组件
    }

    const DOM = (
        <Fragment>
            <div style={contentStyle}>
                <div style={divStyle}>
                    <h1 style={textStyle}>
                        {message}
                    </h1>
                    <button onClick={() => { callBack && callBack(close) }}>确定</button>
                </div>
            </div>
        </Fragment>
    );
    const root = createRoot(app)
    root.render(DOM);
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
React Modal 是一个常用的弹窗组件库,可以用来实现弹窗输入框的功能。下面是一个简单的实现示例: 首先,你需要引入 React Modal: ```javascript import Modal from 'react-modal'; ``` 然后,在你的组件中,你需要定义一个状态来控制弹窗的显示与隐藏: ```javascript function MyComponent() { const [modalIsOpen, setModalIsOpen] = useState(false); const openModal = () => { setModalIsOpen(true); }; const closeModal = () => { setModalIsOpen(false); }; return ( <div> <button onClick={openModal}>打开弹窗</button> <Modal isOpen={modalIsOpen} onRequestClose={closeModal} > <h2>请输入:</h2> <input type="text" /> <button onClick={closeModal}>确定</button> </Modal> </div> ); } ``` 在这个示例中,我们定义了一个状态 `modalIsOpen`,表示弹窗是否打开。当用户点击打开弹窗的按钮时,我们调用 `openModal` 函数来更新状态,打开弹窗。当用户点击弹窗中的确定按钮或者关闭弹窗时,我们调用 `closeModal` 函数来更新状态,关闭弹窗。 在弹窗中,我们可以通过普通的 HTML 元素来实现输入框和按钮。在这个示例中,我们定义了一个输入框和一个确定按钮,用户可以在输入框中输入内容,并点击确定按钮来关闭弹窗。 需要注意的是,在 React Modal 中,我们需要通过 `isOpen` 属性来控制弹窗的显示与隐藏。当 `isOpen` 的值为 `true` 时,弹窗会显示出来;当 `isOpen` 的值为 `false` 时,弹窗会隐藏起来。同时,我们还需要定义一个 `onRequestClose` 回调函数,来处理用户点击弹窗以外的区域时,弹窗自动关闭的情况。 希望这个示例对你有所帮助!

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

臧小川

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值