thanhtunguet / grapesjs-react

A React wrapper for GrapesJS library
https://thanhtunguet.github.io/grapesjs-react
194 stars 58 forks source link

onInit has no access to editor #49

Closed adamflorizone closed 3 years ago

adamflorizone commented 3 years ago

I need access to editor.getHtml(). Should onInit have access to the editor? I have seen https://github.com/thanhtunguet/grapesjs-react/issues/37, but I cant seem to access it.

My code:

<GrapesjsReact
    id='grapesjs-react'
    // onInit={(e) => setTitle(e.target.value)}
    onInit={[onInit]}
    children={<div>test</div>}
    plugins={[
      'gjs-preset-newsletter',
      'gjs-blocks-basic'
    ]}
  />

This Is what I tried:

onInit={console.log(this)}:

Gives:

'editor' is not defined  no-undef

Also would expect that onInit functions more like most callbacks (such as https://reactjs.org/docs/forms.html onChange) and takes a function:

function onInitFunc(editor){
  console.log(editor.getHtml());
}
.....
onInit={onInitFunc}

Gives:

undefined

Same this for:

onInit={(e) => console.log(e)}

Gives:

undefined

How do I access editor.getHtml()?

thanhtunguet commented 3 years ago

You are using the newest version. The onInit function is missing. I am working on it. Please note that onInit is a function so you should not pass it as an array:

<GrapesjsReact
    id='grapesjs-react'
    // onInit={(e) => setTitle(e.target.value)}
// The next line
    onInit={[onInit]}
// should be
    onInit={onInit}
    children={<div>test</div>}
    plugins={[
      'gjs-preset-newsletter',
      'gjs-blocks-basic'
    ]}
  />
thanhtunguet commented 3 years ago

I need access to editor.getHtml(). Should onInit have access to the editor? I have seen #37, but I cant seem to access it.

My code:

<GrapesjsReact
    id='grapesjs-react'
    // onInit={(e) => setTitle(e.target.value)}
    onInit={[onInit]}
    children={<div>test</div>}
    plugins={[
      'gjs-preset-newsletter',
      'gjs-blocks-basic'
    ]}
  />

This Is what I tried:

onInit={console.log(this)}:

Gives:

'editor' is not defined  no-undef

Also would expect that onInit functions more like most callbacks (such as https://reactjs.org/docs/forms.html onChange) and takes a function:

function onInitFunc(editor){
  console.log(editor.getHtml());
}
.....
onInit={onInitFunc}

Gives:

undefined

Same this for:

onInit={(e) => console.log(e)}

Gives:

undefined

How do I access editor.getHtml()?

Please check the new published version v4.0.1-alpha

Screenshot from 2021-04-17 00-03-41