在 draftjs 中获取用户输入的文本
Get the text typed from a user in draftjs
我的 React 应用程序上有一个表单。我想从我的用户的表单中捕获输入并将文本存储在状态 属性 中,然后将文本发送回我的服务器。我遵循了基本的 DraftJS 教程,但是它为我提供了我所在状态的地图,而不仅仅是发送回服务器所需的文本。
constructor(props) {
super(props);
this.state = {
name: '',
teamName: '',
editorState: EditorState.createEmpty(),
teamId: '',
uploadedFileCloudinaryUrl: ''
};
this.handleChange = this.handleChange.bind(this);
this.handleSubmit = this.handleSubmit.bind(this);
this.onChange = (editorState) => this.setState({ editorState });
}
<Editor editorState={this.state.editorState} onChange={this.onChange} />
我需要做些什么才能从编辑那里得到文本吗?
您似乎想向服务器发送一个对象,而您的服务器只接受字符串。在这种情况下,您可以使用 JSON.stringify
。因此,要将编辑器状态发送到服务器,您需要 运行 sendToSeverFunction(JSON.stringify(this.props.editorState))
然后在服务器中解码字符串。
我的 React 应用程序上有一个表单。我想从我的用户的表单中捕获输入并将文本存储在状态 属性 中,然后将文本发送回我的服务器。我遵循了基本的 DraftJS 教程,但是它为我提供了我所在状态的地图,而不仅仅是发送回服务器所需的文本。
constructor(props) {
super(props);
this.state = {
name: '',
teamName: '',
editorState: EditorState.createEmpty(),
teamId: '',
uploadedFileCloudinaryUrl: ''
};
this.handleChange = this.handleChange.bind(this);
this.handleSubmit = this.handleSubmit.bind(this);
this.onChange = (editorState) => this.setState({ editorState });
}
<Editor editorState={this.state.editorState} onChange={this.onChange} />
我需要做些什么才能从编辑那里得到文本吗?
您似乎想向服务器发送一个对象,而您的服务器只接受字符串。在这种情况下,您可以使用 JSON.stringify
。因此,要将编辑器状态发送到服务器,您需要 运行 sendToSeverFunction(JSON.stringify(this.props.editorState))
然后在服务器中解码字符串。