在 reactjs 中将一个组件的值共享到另一个组件
share value from one component into another in reactjs
为此我使用了“React Draft Wysiwyg”。在下面的代码中,我想访问 desktop.js 文件中变量 value
editor.js 文件的值。我该怎么做?
editor.js:
export default class TextEditor extends Component {
render(){
return(){
<textarea
disabled
value={draftToHtml(convertToRaw(editorState.getCurrentContent()))}
></textarea>
);
}
}
desktop.js:
export const Desktop = () => {
return (
<div className="splitScreen">
<TextEditor/>
</div>
}
首先,您需要从桌面组件向 TextEditor 传递一个函数作为道具。
像这样 <TextEditor onChange={this.onChange}>
.
在Desktop组件中声明一个方法如下
onChange = (value) => {
console.log(value);
}
现在在 onEditorStateChange 方法中的 TextEditor 组件中调用此方法,如下所示
onEditorStateChange = (editorState) => {
this.setState({
editorState,
});
this.props.onChange(draftToHtml(convertToRaw(editorState.getCurrentContent()));
};
我建议在 desktop.js 中使用 useState
,同时将 textValue
的状态和 setState
函数作为属性传递给 TextEditor
组件:
import React, { useState } from "react";
export const Desktop = () => {
const [textValue, setTextValue] = useState("");
return (
<div className="splitScreen">
<TextEditor textValue={textValue} setTextValue={setTextValue} />
</div>
);
};
然后使用里面的两个道具TextEditor
:
import React, { Component } from "react";
import { Editor } from "react-draft-wysiwyg";
import { EditorState, convertToRaw } from "draft-js";
import "./editor.css";
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;
// here you set the textValue state for the parent component
this.props.setTextValue(
draftToHtml(convertToRaw(editorState.getCurrentContent()))
);
return (
<div className="editor">
<Editor
editorState={editorState}
toolbarClassName="toolbarClassName"
wrapperClassName="wrapperClassName"
editorClassName="editorClassName"
onEditorStateChange={this.onEditorStateChange}
/>
<textarea
disabled
text_value={this.props.textValue} // here you use the textValue state passed as prop from the parent component
></textarea>
</div>
);
}
}
为此我使用了“React Draft Wysiwyg”。在下面的代码中,我想访问 desktop.js 文件中变量 value
editor.js 文件的值。我该怎么做?
editor.js:
export default class TextEditor extends Component {
render(){
return(){
<textarea
disabled
value={draftToHtml(convertToRaw(editorState.getCurrentContent()))}
></textarea>
);
}
}
desktop.js:
export const Desktop = () => {
return (
<div className="splitScreen">
<TextEditor/>
</div>
}
首先,您需要从桌面组件向 TextEditor 传递一个函数作为道具。
像这样 <TextEditor onChange={this.onChange}>
.
在Desktop组件中声明一个方法如下
onChange = (value) => {
console.log(value);
}
现在在 onEditorStateChange 方法中的 TextEditor 组件中调用此方法,如下所示
onEditorStateChange = (editorState) => {
this.setState({
editorState,
});
this.props.onChange(draftToHtml(convertToRaw(editorState.getCurrentContent()));
};
我建议在 desktop.js 中使用 useState
,同时将 textValue
的状态和 setState
函数作为属性传递给 TextEditor
组件:
import React, { useState } from "react";
export const Desktop = () => {
const [textValue, setTextValue] = useState("");
return (
<div className="splitScreen">
<TextEditor textValue={textValue} setTextValue={setTextValue} />
</div>
);
};
然后使用里面的两个道具TextEditor
:
import React, { Component } from "react";
import { Editor } from "react-draft-wysiwyg";
import { EditorState, convertToRaw } from "draft-js";
import "./editor.css";
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;
// here you set the textValue state for the parent component
this.props.setTextValue(
draftToHtml(convertToRaw(editorState.getCurrentContent()))
);
return (
<div className="editor">
<Editor
editorState={editorState}
toolbarClassName="toolbarClassName"
wrapperClassName="wrapperClassName"
editorClassName="editorClassName"
onEditorStateChange={this.onEditorStateChange}
/>
<textarea
disabled
text_value={this.props.textValue} // here you use the textValue state passed as prop from the parent component
></textarea>
</div>
);
}
}