私は、ユーザーを有効にするためにdraft.jsエディターに読み込みしようとしているconvertToRawを使用して、コンテンツを再編集します。draft.jsのeditorStateを設定できません(不変ですがエラーは表示されません)
draft.jsエディタは、ユーザーがコンテンツの「編集」をクリックすると表示されるreact-modalベースのコンポーネントに含まれています。これは、モーダル(およびエディタ)が再インスタンス化されず、表示され、非表示になっているため重要です。
エディタは、単に使用して(ES6)クラスのコンストラクタに一度開始されます。
this.state = {editorState: EditorState.createEmpty()}
を私はDBからの生のコンテンツをロードすると、ユーザーがクリック「編集」、そして、私は私のロードを試してみました編集者への生のコンテンツの変異体の数使用:
const contentState = convertFromRaw(rawContent)
const newEditorState = EditorState.push(this.state.editorState, contentState);
this.setState({editorState: newEditorState})
をしかしnewEditorStateは明らかに正しい内容が含まれていながら、this.setState({editorState: newEditorState})
は、コンポーネント(または編集)の状態に全く影響を与えないように思われます。
エディタの新しい状態を設定する方法はありますか?ありがとう!さらに調査に
UPDATE 、それは明らかに単にだけエディタコンポーネントのために失敗しているthis.setState({editorState: newEditorState})
。
私はテスト状態のプロパティを設定し、それを正常に更新することでテストしましたが、editorStateは変更されません。完全にこれをテストする
、私のコンストラクタで、私が使用してテスト値と状態を初期化しました。その後、私はSETSTATEのための私のテスト値の作品ではなく、どのように表示するには、いくつかのテストコードを書いている
this.state = {
editorState:EditorState.createWithContent(ContentState.createFromText('Hello')),
testVal: 'Test Val 1'
}
をdraft.js editorState:
Before setState
newEditorState: Goodbye
editorState: Hello
testVal: Test Val 1
After setState
editorState: Hello
testVal: Test Val 2
I:
const newEditorState = EditorState.createWithContent(ContentState.createFromText('Goodbye'))
console.log('Before setState')
console.log('newEditorState: ' + newEditorState.getCurrentContent().getPlainText());
console.log('editorState: ' + this.state.editorState.getCurrentContent().getPlainText());
console.log('testVal: ' + this.state.testVal);
this.setState({editorState: newEditorState, testVal: 'Test Val 2'}, function(){
console.log('After setState')
console.log('editorState: ' + this.state.editorState.getCurrentContent().getPlainText());
console.log('testVal: ' + this.state.testVal);
});
コンソール出力は次のようになりますtestValがいつdraft.js editorStateが更新されていないのか分かりません。
Thanks @ jiang-YD私の問題はthis.setStateにあるようですが、状態を更新するだけでは表示されません。 –