React-Call让你的React组件像异步函数一样调用

在React项目的开发过程中,很多时候像Toast 、Confirm 、Notification 、Drawer等等组件我们预期的是非必要可以不需要 ,只有在使用的时候才需要渲染。像window.confirm 一样的操作。 虽然很多React的组件库都实现了,比如Antd 的Modal 组件实现了Modal.confirm等。

但是平时我们不想引入组件库只想通过Tailwindcss或者原生的css写的时候就可以通过React-Call这个组件库就可以实现对应的功能:官网

创建Confirm组件

Confirm.tsx 复制代码
import { createCallable } from 'react-call'

interface Props {

    message: string

}

type Response = boolean

export const Confirm = createCallable<Props, Response>(({ call, message }) => (

    <div

    role="dialog"

    aria-modal="true"

    className="fixed inset-0 z-50 flex items-center justify-center bg-black/50 backdrop-blur-sm"

    >

    <div className="w-full max-w-sm rounded-lg border border-[var(--color-border)] bg-[var(--color-bg)] p-6 shadow-2xl">

    <p className="text-base text-[var(--color-fg)]">{message}</p>

    <div className="mt-6 flex items-center justify-end gap-3">

        <button

        type="button"

        onClick={() => call.end(false)}

        className="rounded-md border border-[var(--color-border)] px-4 py-2 text-sm font-medium text-[var(--color-fg-muted)] transition-colors hover:text-[var(--color-fg)]" >

            取消

        </button>

        <button

        type="button"

        onClick={() => call.end(true)}

        className="rounded-md bg-[var(--color-accent)] px-4 py-2 text-sm font-medium text-[var(--color-accent-fg)] transition-colors hover:bg-[var(--color-accent-hover)]"

        >

            继续

        </button>

    </div>

    </div>

    </div>

))

Confirm.displayName = 'Confirm'

将组件引入到root中

js 复制代码
import { Confirm } from './Confirm'

import { DeleteButton } from './DeleteButton'

export default function App() {

return (

<>

<Confirm />

<DeleteButton />

</>

)

}

在项目的任何地方引用创建的Comfirm组件

js 复制代码
import { useState } from 'react'

import { Confirm } from './Confirm'

export const DeleteButton = () => {

const [status, setStatus] = useState<'idle' | 'deleted' | 'cancelled'>('idle')

const handleClick = async () => {

    const accepted = await Confirm.call({

    message: '确定要删除这条数据?',

    })

    setStatus(accepted ? 'deleted' : 'cancelled')

}

return (

<div className="flex flex-col items-center gap-3">

    <button

    type="button"

    onClick={handleClick}

    className="rounded-md bg-red-600 px-4 py-2 text-sm font-medium text-white transition-colors hover:bg-red-700"

    >

        删除

    </button>

    <span className="font-mono text-xs text-[var(--color-fg-subtle)]">

    {status === 'idle' && '→ awaiting click...'}

    {status === 'deleted' && (

        <span className="text-[var(--color-accent)]">→ deleted</span>

    )}

        {status === 'cancelled' && '→ cancelled'}

    </span>

</div>

)

}
相关推荐
用户2986985301414 分钟前
在 React 中实现 Excel 与 TXT 格式互转的前端方案
javascript·react.js·excel
涛涛ing31 分钟前
我把项目里的 Zustand 全删了:代码量减半,Bug 少了,性能还起飞了
前端
map_3d_vis1 小时前
百度地图地图类型切换:普通地图、卫星图、混合图
前端·javascript·地图·百度地图
圆心角12 小时前
代码也有折旧率
前端·程序员
CoderYanger12 小时前
Java EE 进阶:14.案例综合练习-博客系统(第二弹):博客详情+令牌技术
服务器·前端·spring boot·后端·spring·java-ee·github
kyriewen15 小时前
我测了 5 种关页上报,两种老写法一条都没发出去
前端·javascript·程序员
不一样的少年_18 小时前
受够了当人肉传话筒:我把这 9 个 Agent 塞进了同一个聊天框
前端·agent·ai编程
狗头大军之江苏分军19 小时前
《潮水漫过十七岁》 加一个蛋,顺便说一下我解放了
前端·后端
Captaincc19 小时前
掘金AI用量迎来重大更新!全新AI用量
前端·后端
code2cat19 小时前
React Native入门:用Expo在手机上录入第一条任务
react native·react.js·智能手机