我一直在try 在Next中使用"Reaction-Email-EDITOR".我想将在编辑器中创建的邮箱内容复制到剪贴板.由于该包使用"Window"进行交互,所以我try 使用Next/Dynamic来禁用SSR.因此,我try 为EmailEditor创建不同的组件.但当我试图记录emailEditorRef时,它从未指向EmailEditor.我需要emailEditorRef来复制邮箱编辑器中的内容.

我使用的版本是:"REACT":"18.2.0","NEXT":"13.4.19","REACT-EMAIL-EDITOR":"^1.7.9".

我将主逻辑划分为3个文件:page.tsx、tyes.tsx、mail-ui.tsx.

page.tsx

'use client';

import { Button } from '@/components/ui/button';
import { toast } from '@/components/ui/use-toast';
import React, { useEffect, useRef } from 'react';
import { EditorRef, EmailEditorProps } from '@/components/email-template/types';
import ForwardedEmailEditor from '@/components/email-template/email-ui';

export default function Page() {
    const emailEditorRef = useRef<EditorRef | null>(null);

    useEffect(() => {
        console.log('ref changed = ', emailEditorRef.current);
    }, [emailEditorRef]);

    const exportHtml = () => {
        const unlayer = emailEditorRef.current?.editor;
        unlayer?.exportHtml((data) => {
            const { design, html } = data;
            try {
                console.log('Copy function Running',html);
                navigator.clipboard.writeText(html);
            } catch (error) {
                console.error('Clipboard write failed:', error);
            }
        });
    };

    const onReady: EmailEditorProps['onReady'] = (unlayer) => {
        console.log('editor react-email-editor is ready', emailEditorRef.current);
    };

    return (
        <section >
            <div>
                <span>Create Email Template</span>
                <Button onClick={exportHtml}>Export HTML</Button>
            </div>
            <div>
                <ForwardedEmailEditor ref={emailEditorRef} onReady={onReady} />
            </div>
        </section>
    );
}`

email-ui.tsx

'use client';

import React from 'react';
import dynamic from 'next/dynamic';
import { EmailEditorProps, EditorRef } from './types';

const DynamicEmailEditor = dynamic(() => import('react-email-editor'), { ssr: false });

const ForwardedEmailEditor = React.forwardRef<EditorRef, EmailEditorProps>((props, ref) => {
    return <DynamicEmailEditor ref={ref} {...props} />;
});

export default ForwardedEmailEditor;

types.tsx

'use client';

export type { EditorRef, EmailEditorProps } from 'react-email-editor';

推荐答案

我发现PARAMS onReady函数中的值UNLAYER是editor类型,所以我使用了它,并将值emailEditorRef.current更改为指向编辑器.

100

...
    const onReady: EmailEditorProps['onReady'] = (unlayer) => {
        emailEditorRef.current = { editor: unlayer };
    };
...

并从Email-ui.tsx页面中删除了不必要的代码,作为 EmailEditor等于React.ForwardRefExoticComponent

100

'use client';

import dynamic from 'next/dynamic';

const DynamicEmailEditor = dynamic(() => import('react-email-editor'), { ssr: false });

export default DynamicEmailEditor;

Typescript相关问答推荐

TypeScript泛型参数抛出错误—?&#"' 39;预期"

将props传递给子组件并有条件地呈现它''

类型{...}的TypeScript参数不能赋给类型为never的参数''

打印脚本丢失函数的泛型上下文

如何键入函数以只接受映射到其他一些特定类型的参数类型?

如何使用WebStorm调试NextJS的TypeScrip服务器端代码?

Angular 15使用react 式表单在数组内部创建动态表单数组

限制返回联合的TS函数的返回类型

在列表的指令中使用intersectionObservable隐藏按钮

如何从MAT-FORM-FIELD-MAT-SELECT中移除焦点?

TYPE FOR ARRAY,其中每个泛型元素是单独的键类型对,然后在该数组上循环

如何通过属性名在两个泛型数组中找到匹配的对象?

作为函数参数的联合类型的键

如何使函数参数数组不相交?

使用泛型以自引用方式静态键入记录的键

无法使用prisma中的事务更新记录

递归类型名称

Mongodb TypeError:类继承 events_1.EventEmitter 不是对象或 null

req.files = 未定义(Multer、Express、Typescript)

从接口推断模板参数?