【发布时间】:2018-05-02 01:44:45
【问题描述】:
我正在尝试将 Draft.js 的编辑器状态从编辑器组件传递到我自己的 Sidebar 组件。
使用最顶层组件Notes 我使用回调从CustomEditor 获取编辑器状态并将其设置为Notes 状态。然后我将该状态作为道具传递给Sidebar。
问题是属性是在回调触发之前设置的。我在想setTimeout,但这似乎很粗糙。我知道UNSAFE_componentWillReceiveProps(),但文档不推荐它。这个用例有什么反应吗?
export class Notes extends Component {
constructor(props) {
super(props);
this.getEditorState = this.getEditorState.bind(this)
this.state = {
editorState: "the placeholder data Sidebar should not have as a prop"
};
}
getEditorState(state) {
console.log(state)
this.setState({editorState: state})
}
render() {
return (
<section id="Notes">
<div id="editor-holder">
<Sidebar currentEditorState={this.state.editorState}/>
<div id="Editor">
<FileHeader />
<CustomEditor getState={this.getEditorState}/>
</div>
</div>
</section>
);
}
}
export default Notes;
【问题讨论】:
标签: reactjs react-props