我想要一个呈现Slate编辑器的页面,从 API 检索文档,然后在编辑器更新并完成重新呈现时打印窗口。如何识别编辑器何时重新渲染?
示例代码:
componentDidMount() {
$.get('/api/')
.done((data) => {
this.setState({ document: Value.fromJSON(data) });
// workaround to try and make sure document is rendered
// what can I do here instead??
setTimeout(() => window.print(), 500);
});
}
render() {
const { document } = this.state;
return (
<Editor
value={document}
readOnly
...
/>
);
我尝试componentDidUpdate
在父组件中使用:
componentDidUpdate(prevProps, prevState) {
window.print();
}
但是该函数在编辑器完全呈现之前被触发,因此文本不会显示在“打印”对话框中: