简体   繁体   English

如何从一个组件获取价值到另一个组件?

[英]How can I get the value from a component to another?

This is my TextEditor.js.这是我的 TextEditor.js。 I am willing to use it wherever I want it but when I use this component I am wishing to get the value associated with it every time it's state gets change.我愿意在任何我想要的地方使用它,但是当我使用这个组件时,我希望每次 state 发生变化时都获得与它相关的值。

import React, { Component } from "react";
import { Editor } from "react-draft-wysiwyg";
import { EditorState, convertToRaw } from "draft-js";

import "react-draft-wysiwyg/dist/react-draft-wysiwyg.css";
import draftToHtml from "draftjs-to-html";

export default class TextEditor extends Component {
    state = {
        editorState: EditorState.createEmpty(),
    };

    onEditorStateChange = (editorState) => {
        this.setState({
            editorState,
        });
    };

    render() {
        const { editorState } = this.state;
        console.log(draftToHtml(convertToRaw(editorState.getCurrentContent())));

//I want to get draftToHtml(convertToRaw(editorState.getCurrentContent())) whenever I call this component

        return (
            <div>
                <Editor
                    editorState={editorState}
                    toolbarClassName="toolbarClassName"
                    wrapperClassName="wrapperClassName"
                    editorClassName="editorClassName"
                    onEditorStateChange={this.onEditorStateChange}
                />
     
            </div>
        );
    }
}

I want to use it in below component.我想在下面的组件中使用它。 I want to use it so that I can get the value whenever its state get changed and I can save the data.我想使用它,以便在其 state 发生更改时获取该值,并且可以保存数据。

import React, {useState} from 'react';
import axios from "axios"

import ReactDOM from 'react-dom';
import {Editor, EditorState,RichUtils} from 'draft-js';
import 'draft-js/dist/Draft.css';
import TextEditor from '../TextEditor';

function AddBlog() {
    

  return <div>
      <h3 className='text-center my-3'>
            Add blog
      </h3><hr/>
      <div className='col-lg-6 mx-auto'>
      <form>
 
  <div class="mb-3 border p-2 rounded-3">
    <label for="exampleInputPas1" class="form-label">Description</label>
    <input type="text" hidden  value={desc} onChange={(e)=>setDesc(e.target.value)} class="form-control" id="exampleInputrd1" />
    <div className="editor">

        <TextEditor  />
//I want the value from TextEditor so that I can save it.
      </div>
 
  </div>
  
  <div className='d-flex justify-content-end'>

  <button type="submit" onClick={submitForm} class="btn btn-danger btn-sm">Add</button>
  </div>
</form>

      </div>

  </div>;
}

export default AddBlog;

So how can I get the value form TextEditor.js to AddBlog.js so that I can save it.那么如何将 TextEditor.js 的值获取到 AddBlog.js 以便我可以保存它。

There are two solutions有两种解决方案

  1. use redux使用 redux
  2. create another file which stores the text, the functionallity would be something like when the user clicks save, the text is saved into the other file and you can reference it in AddBlog.js创建另一个存储文本的文件,功能类似于当用户单击保存时,文本将保存到另一个文件中,您可以在 AddBlog.js 中引用它
  1. Lift the editorState state up to the AddBlog component.editorState state 提升到AddBlog组件。
  2. Pass a onEditorStateChange callback from AddBlog to TextEditor and it will set the editor state in AddBlog on edit.将来自AddBlogonEditorStateChange回调传递给TextEditor ,它将在编辑时在AddBlog中设置编辑器 state。

TextEditor文本编辑器

export default class TextEditor extends Component {
 
  render() {
    console.log(draftToHtml(convertToRaw(this.props.editorState.getCurrentContent())));

    return (
      <div>
        <Editor
          editorState={this.props.editorState}
          onEditorStateChange={this.props.onEditorStateChange}
        />
      </div>
    );
  }
}

AddBlog添加博客

import {useState} from 'react'
import { EditorState } from "draft-js";

function AddBlog() {
  const [editorState,setEditorState] = useState(EditorState.createEmpty())

  const onEditorStateChange = (editorState) => {
    setEditorState(editorState)
  }

  return (
    <div>
      <TextEditor onEditorStateChange={onEditorStateChange} editorState={editorState}/>
    </div>
  );
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM