globocom / react-native-draftjs-render

React Native render for draft.js model
MIT License
390 stars 62 forks source link

How to get the contentState from an EditorState instance ? #52

Closed teod closed 5 years ago

teod commented 5 years ago

I have an EditorState instance created with EditorState.createWithContent(). When I try to pass editorState.getCurrentContent() as contentState to the getRNDraftJSBlocks function it just returns null.

teod commented 5 years ago

so you shouldn't pass an actual ContentState instance, but the result from convertToRaw: getRNDraftJSBlocks({ contentState: convertToRaw(editorState.getCurrentContent()) })

Thanks for a great library ! but this part was really confusing, the docs are too incomplete.

malonguwa commented 5 years ago

@teod hey man, could you mind to share more complete code where to import EditorState? and use it? I am very new to the draftjs... I would like to give it a try in my current react native app, I followed the docs.. but no luck so far.... Thanks a lot

teod commented 5 years ago

@malonguwa well EditorState is a module from draftjs rather than this package, so basically that's an instance you would normally pass to a draftjs Editor component.

A basic working example with draft-js and react-native-draftjs-render will look like something similar:

import EditorState from 'draft-js/lib/EditorState'
import convertFromRaw from 'draft-js/lib/convertFromRawToDraftState'
import convertToRaw from 'draft-js/lib/convertFromDraftStateToRaw'
import getRNDraftJSBlocks from 'react-native-draftjs-render'

const editorState = EditorState.createWithContent(
        convertFromRaw({
          blocks: [{
             inlineStyleRanges: [],
             text: 'This is just an unstyled raw block',
             type: 'unstyled'
          }],
          entityMap: {},
        })
      )

getRNDraftJSBlocks({ contentState: convertToRaw(editorState.getCurrentContent()) })