【问题标题】:How to get the html content from react-draft-wysiwyg from the child component to the parent?如何从子组件到父组件的 react-draft-wysiwyg 获取 html 内容?
【发布时间】:2021-08-07 07:07:39
【问题描述】:

我正在尝试将我在编辑器中键入的 html 内容从编辑器组件(子)获取到父级,以便我可以将表单与编辑器的内容一起保存到我的数据库中。

我尝试将 getContent 函数作为道具发送到编辑器(子)并从编辑器取回内容,但我不断收到错误消息,显示 getContent is not a function

这样做的正确方法是什么,以便我可以将 html 内容从编辑器获取到父组件?

Editor.js(子)

import { useState } from 'react';
import { Editor } from 'react-draft-wysiwyg';
import { EditorState, convertToRaw } from 'draft-js';
import draftToHtml from 'draftjs-to-html';
import 'react-draft-wysiwyg/dist/react-draft-wysiwyg.css';
import './RichTextEditor.css';

const Editor = (getContent) => {
    const [editorState, setEditorState] = useState(() =>
        EditorState.createEmpty()
    );

    const handleEditorChange = (state) => {
        setEditorState(state);
        sendContent();
    };

    const sendContent = () => {
        getContent(draftToHtml(convertToRaw(editorState.getCurrentContent())));
    };

    return (
        <div>
            <Editor
                editorState={editorState}
                onEditorStateChange={handleEditorChange}
                wrapperClassName="wrapper-class"
                editorClassName="editor-class"
                toolbarClassName="toolbar-class"
            />
            <textarea value={draftToHtml(convertToRaw(editorState.getCurrentContent()))}></textarea>
        </div>
    );
};

export default Editor;

CreateBulletin(父级)

import { useEffect, useState } from 'react'
import { Button, Col, Row } from 'react-bootstrap'
import Editor from '../Editor/Editor'

const AddBulletin = () => {
    const [title, setTitle] = useState ('')
    const [htmlContent, setHtmlContent] = useState('')

    const getContent = (htmlContentProp) => {
        setHtmlContent(htmlContentProp);
        console.log(htmlContentProp);
    }

    const onSubmit = (e) => {
        e.preventDefault();

        if (!title) {
            alert('Please add a title')
            return
        }

        onAdd({ title, htmlContent })

        setTitle('')
        setHtmlContent('')
    }

    return (
        <Row>
            <Col xs={12} md={8}>
                <form className='add-form' onSubmit={ onSubmit}>
                    <div className='form-control bulletin'>
                        <label>Title</label>
                        <input type='text' placeholder='Insert your title here...' style={{width: '100%'}}
                        value={title} onChange={(e) => setTitle(e.target.value)} />
                    </div>
                    <div className='form-control bulletin'>
                        <label>Content</label>
                        <Editor getContent={getContent} />
                    </div>
                    <Button as="input" type="submit" value="Submit" />
                    <Button variant="outline-danger">Cancel</Button>
                </form>
            </Col>
        </Row>
    )
}

export default AddBulletin

【问题讨论】:

    标签: reactjs


    【解决方案1】:

    组件 props 始终是一个对象。您收到的任何道具都将是props 对象的属性。

    你可以这样使用

    const Editor = (props) => {
    ... 
       props.getContent(...);
    ...
    

    const Editor = ({getContent}) => {
    ... 
       getContent(...);
    ...
    

    【讨论】:

    • 啊,我明白了,谢谢!我实际上错过了子组件中 getContent 的花括号,这就是它不起作用的原因。
    猜你喜欢
    • 1970-01-01
    • 2020-03-25
    • 1970-01-01
    • 1970-01-01
    • 2022-06-16
    • 1970-01-01
    • 1970-01-01
    • 2021-10-26
    • 1970-01-01
    相关资源
    最近更新 更多