javascript - ReactJS如何访问组件的内容输出?
问题描述
我目前正在开发一个使用 QuillJS 作为富文本编辑器的项目。我需要将富文本内容发布到我的后端,但我不确定如何访问 QuillJS 输出。
在 RichTextEditor.js 中
import React, { Component } from "react";
import ReactQuill from "react-quill";
import "react-quill/dist/quill.snow.css";
class RichTextEditor extends Component {
constructor(props) {
super(props);
// this.formats = formats;
this.state = { text: "" }; // You can also pass a Quill Delta here
this.handleChange = this.handleChange.bind(this);
}
handleChange(value) {
this.setState({ text: value });
const text = this.state;
console.log(text);
}
render() {
return (
<ReactQuill
value={this.state.text}
onChange={this.handleChange}
formats={this.formats}
modules={this.modules}
/>
);
}
}
export default RichTextEditor;
console.log(text)
基本上只是输出富文本编辑器的内容。像这样的东西"<p><em>aasdasdasd</em><strong><em>asdasdasdasd</em></strong></p>"
在 Post.js 中
import React, { Component } from "react";
import RichTextEditor from "./RichTextEditor.js";
import "../../css/Post.css";
class Post extends Component {
constructor(props) {
super(props);
this.state = {
question: "",
};
}
onChange = (e) => {
this.setState({ [e.target.name]: e.target.value });
console.log(this.state);
};
handleSubmit = (e) => {
e.preventDefault();
const { question } = this.state;
console.log("Question");
console.log(question);
render() {
const { question } = this.state;
return (
<div className="post">
<div className="post__container">
<form onSubmit={this.handleSubmit}>
<div className="post__richTextEditor">
<RichTextEditor value={question} onChange={this.onChange} name="question" />
</div>
</form>
</div>
</div>
);
}
}
export default Post;
我正在尝试更新问题的状态,但似乎没有更新。console.log(question)
只输出一个字符串。
如何从 RichTextEditor.js 访问相同的字符串输出?
解决方案
您的 RichTextEditor 组件不应该处理更改,它应该只接收来自更高组件的道具:
class RichTextEditor extends Component {
constructor(props) {
super(props);
}
render() {
return (
<ReactQuill
value={this.props.value}
onChange={this.props.onChange}
formats={this.formats}
modules={this.modules}
/>
);
}
}
export default RichTextEditor;
然后你的 Post 组件传递value
和onChange
props 到 RichTextEditor:
class Post extends Component {
constructor(props) {
super(props);
this.state = {
question: "",
};
this.onChange = this.onChange.bind(this);
}
onChange = (e) => {
this.setState({ [e.target.name]: e.target.value });
console.log(this.state);
};
handleSubmit = (e) => {
e.preventDefault();
const { question } = this.state;
console.log("Question");
console.log(question);
render() {
const { question } = this.state;
return (
<div className="post">
<div className="post__container">
<form onSubmit={this.handleSubmit}>
<div className="post__richTextEditor">
<RichTextEditor value={question} onChange={this.onChange} name="question" />
</div>
</form>
</div>
</div>
);
}
}
推荐阅读
- python - 如何打印用户想要的某个字符串的字母
- flutter - 从 flutter_bluetooth_serial 依赖项运行 getBondedDevices() 函数时应用程序崩溃
- android - Jetpack Compose - 如何在可滚动的非惰性列中控制 IME(键盘)?
- python - 仅使用 Colab Pro 的 GPU
- r - 将 gt 表保存为没有 phantomjs 的 PNG
- r - 使用 RcppArmadillo 源 (sourceCpp()) 文件时出错
- flutter - Flutter中如何从listview中获取数据
- python - 错误:“只有 unicode 对象是可转义的。没有类型
“。有人可以帮我找出我的代码中的错误吗? - css - 材质 UI 日期选择器伸出
- python - 从 Python openCV 中每一帧的视频中获取经过的时间