Open tombear110 opened 5 years ago
npm的版本,用这段代码,未知原因不能成功setValue
需要重新获得instance之后,然后instance.setValue(state)才可以
class DemoBraftEditor extends React.Component{
state:ComponentState = {
editorState: BraftEditor.createEditorState(null),
}
editorInstance:BraftEditor|undefined
...
uploadHandler = (param:UploadFile) => {
this.setState((prevState: ComponentState) => ({
editorState: ContentUtils.insertMedias(prevState.editorState, [
{
type: 'IMAGE',
url: URL.createObjectURL(param.file),
},
]),
}));
this.editorInstance?.setValue(this.state.editorState);
}
render(){
...
return(
...
<BraftEditor
id="demo-editor-with-emoticon"
// eslint-disable-next-line no-return-assign
ref={instance => (this.editorInstance = instance)}
className={styles.myEditor}
placeholder="请输入正文内容"
value={this.state.editorState}
onChange={this.handleChange}
customRequest={this.uploadHandler}
/>
...
)
}
}
手动setValue应该就可以了