我试图将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;
答案 0 :(得分:1)
有更多可能的选择如何实现这个结果
只有当道具改变了那个menas
时才能渲染<Sidebar>
constructor(props)
super(props)
this.state = {
editorState: false
}
}
render() {
... {this.state.editorState && <Sidebar currentEditorState={this.state.editorState}/>}
}
<强> Sidebar.js 强>
render() {
if(!this.props.currentEditorState) return null // this will force React to render nothing
return ( ... )
}
https://reactjs.org/docs/react-component.html#static-getderivedstatefromprops
<强> Sidebar.js 强>
static getDerivedStateFromProps({ currentEditorState }, prevState) {
if(currentEditorState !== false {
return { currentEditorState }
} else {
return {}
}
}
render() {
(.... something bound to this.state.currentEditorState)
}
class Notes extends React.Component {
getEditorState(state) {
console.log(state)
this.setState({editorState: state})
}
getChildContext() {
return {
editorState: this.state.editorState
}
}
childContextTypes = {
editorState: PropTypes.oneOfType([PropTypes.obj, PropTypes.bool])
}
}
<强> Sidebar.js 强>
class Sidebar {
static contextTypes = {
editorState: PropTypes.oneOfType([PropTypes.obj, PropTypes.bool])
}
render() {
... this.context.editorState
}
}
答案 1 :(得分:1)
新的Context API是解决此类问题的方法。我想了解一下,但我想到的是editorState
作为道具获得Sidebar
。
export const NoteContext = React.createContext("placeholderEditorState");
export class Notes extends Component {
constructor(props) {
super(props);
this.getEditorState = this.getEditorState.bind(this)
this.getFolderData = this.getFolderData.bind(this)
this.state = {
editorState: null,
folderData: null
};
}
getEditorState(state) {
this.setState({editorState: state});
}
getFolderData(data) {
this.setState({folderData : data})
}
render() {
return (
<section id="Notes">
<TopBar />
<div id="editor-holder">
<NoteContext.Provider value={{editorState: this.state.editorState}} >
<NoteContext.Consumer>
{(context)=>{ return (
<Sidebar currentEditorState={context.editorState} getFolderData={this.getFolderData}/>
)}}
</NoteContext.Consumer>
</NoteContext.Provider>
<div id="Editor">
<NoteContext.Provider value={{folderData: this.state.folderData}} >
<FileHeader />
</NoteContext.Provider>
<CustomEditor getState={this.getEditorState}/>
</div>
</div>
</section>
);
}
}
现在看着它看起来非常简单,这意味着我已经学到了很多东西!如果我能在这里做任何改进,请告诉我。