Javascript 如何知道Draft.js插件何时准备就绪?
我正在使用Draft.js插件 我正在尝试从本地存储加载内容,然后链接它 现在我必须使用Javascript 如何知道Draft.js插件何时准备就绪?,javascript,reactjs,draftjs,Javascript,Reactjs,Draftjs,我正在使用Draft.js插件 我正在尝试从本地存储加载内容,然后链接它 现在我必须使用setTimeout等待linkifyPlugin就绪。否则,加载的内容将是未链接的纯文本 有没有类似事件的方法可以让我知道插件什么时候准备好了 class MyComponent extends Component { constructor(props) { super(props); this.state = { editorState: EditorState.cre
setTimeout
等待linkifyPlugin就绪。否则,加载的内容将是未链接的纯文本
有没有类似事件的方法可以让我知道插件什么时候准备好了
class MyComponent extends Component {
constructor(props) {
super(props);
this.state = {
editorState: EditorState.createEmpty()
};
}
componentDidMount() {
// ...
if (hasDraft) this.loadEditor(draftFromLocalStorage);
}
loadEditor = rawContent => {
// here I have to use setTimeout to wait linkifyPlugin ready
setTimeout(() => {
const editorState = EditorState.push(this.state.editorState, convertFromRaw(rawContent));
this.setState({ editorState });
}, 5);
};
render() {
return (
<Editor
editorState={editorState}
plugins={[linkifyPlugin]}
onChange={this.onEditorChange} />
);
}
}
类MyComponent扩展组件{
建造师(道具){
超级(道具);
此.state={
editorState:editorState.createEmpty()
};
}
componentDidMount(){
// ...
if(hasDraft)this.loadEditor(draftFromLocalStorage);
}
loadEditor=rawContent=>{
//在这里,我必须使用setTimeout来等待插件就绪
设置超时(()=>{
const editorState=editorState.push(this.state.editorState,convertFromRaw(rawContent));
this.setState({editorState});
}, 5);
};
render(){
返回(
);
}
}
尝试使用承诺
loadEditor(rawContent){
return new Promise((resolve, reject) => {
let editorState = EditorState.push(this.state.editorState, convertFromRaw(rawContent));
resolve(editorState);
});
}
//call it after component has mounted
componentDidMount(){
this.loadEditor(draftFromLocalStorage).then(data => this.setState({ editorState: data }));
}
尝试使用
承诺
loadEditor(rawContent){
return new Promise((resolve, reject) => {
let editorState = EditorState.push(this.state.editorState, convertFromRaw(rawContent));
resolve(editorState);
});
}
//call it after component has mounted
componentDidMount(){
this.loadEditor(draftFromLocalStorage).then(data => this.setState({ editorState: data }));
}
奇怪的是,看看源代码,没有理由认为插件有异步代码: 您可以尝试在此函数上设置断点:
该函数将为状态中的每个块调用,因此您应该能够看到文本是否由Linkify处理。如果您删除了超时且函数仍在调用中,那么问题应该在其他地方(因为这意味着您在渲染方面有问题,而不是在实际处理方面)。奇怪的是,查看源代码没有理由认为插件有异步代码: 您可以尝试在此函数上设置断点:
该函数将为状态中的每个块调用,因此您应该能够看到文本是否由Linkify处理。如果您删除了超时并且函数仍在被调用,那么问题应该在其他地方(因为这意味着您在渲染方面有问题,而不是在实际处理方面)。谢谢,我试过了,但没有帮助。问题是我正在等待插件就绪,而不是editorState就绪。如果没有
setTimeout
,则是this.state.editorState===undefined
?this.state.editorState
有值。我猜插件是在编辑器之后加载的。谢谢,我试过了,但没有帮助。问题是我正在等待插件就绪,而不是editorState就绪。如果没有setTimeout
,则是this.state.editorState===undefined
?this.state.editorState
有值。我猜插件是在编辑器之后加载的。