2016-08-02 9 views
0

私は、ユーザーを有効にするために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が更新されていないのか分かりません。

答えて

1

で次でそれをやりました。

私はsetState()を呼び出そうと後すぐにエディタにフォーカスを設定しました。

つまり、私は私がSETSTATEにしてみてくださいfocus()コールを移動することで、エディタ上focus()を呼んでいた、それはすべて働きました。明らかにフォーカスを受け入れると、editorStateに影響があります。

1

私はOK、問題が何であったか、私を見つけた私のプロジェクト

const blocks = convertFromRaw(props.rawBlocks); 
editorState = EditorState.createWithContent(blocks, null); 
+0

Thanks @ jiang-YD私の問題はthis.setStateにあるようですが、状態を更新するだけでは表示されません。 –

関連する問題