1 回答
TA贡献1825条经验 获得超4个赞
问题在于,在每个渲染上都会创建一个新的去抖动功能,因此会多次调用这些API。您必须useCallback用来记忆去抖功能。如果要editorState在已删除反跳的函数中使用,可以onEditSatateChange在调用时将其从方法传递debounced。另外,您需要更正您的反跳语法
const EditorComponent = () => {
const [editorState, setEditorState] = React.useState(
EditorState.createEmpty()
);
// I need another logic which checks the time difference of idling.
const debounced = useCallback(debounce(() => {
console.log("the api is going to call after 5 seconds");
}, 5000), []);
const onEditorStateChange = value => {
const rawContent = convertToRaw(value.getCurrentContent());
const markdown = draftToMarkdown(rawContent);
setEditorState(value);
debounced()
};
return (
<div style={{ width: "500px" }}>
<Editor
editorState={editorState}
toolbarClassName="toolbarClassName"
wrapperClassName="wrapperClassName"
editorClassName="editorClassName"
onEditorStateChange={onEditorStateChange}
/>
</div>
);
};
export default EditorComponent;
添加回答
举报