react-email-editor icon indicating copy to clipboard operation
react-email-editor copied to clipboard

OnLoad function call

Open Mabloq opened this issue 9 years ago • 4 comments

Hey guys, Im unable to load JSON design via onLoad prop. However, I am able to load JSON manually using the same function called by the onLoad prop by pressing the load button once the editor is loaded. Below is my code an error message. Im loading in the JSON via redux but I make sure the template json data is defined before rendering EmailEditor.

class Template extends Component {
  componentDidMount () {
    Promise.resolve (this.props.getTemplate ('emaily'));
  }
  loadDesign = () => {
    this.editor.loadDesign (this.props.template);
  }
  render () {
    return (
         // manual load button
        <button onClick={() => this.loadDesign ()} className="btn right">
            load
        </button>
        <EmailEditor
          onLoad={() => this.loadDesign()}
          //or
          //onLoad={this.loadDesign}}
          ref={editor => (this.editor = editor)}
        />
      </div>
    );
  }
}

function mapStateToProps (state) {
  return {template: state.template.currentTemplate};
}
export default connect (mapStateToProps, actions) (TemplateRF);

Error message if .....onLoad={this.loadDesign} Uncaught (in promise) Error: Minified

Error message if .....onLoad={() => this.loadDesign() Uncaught TypeError: Cannot read property 'get' of undefined at editor.js:2 at l.configureFinalMapState (editor.js:2) at l.computeStateProps (editor.js:2) at l.updateStatePropsIfNeeded (editor.js:2) at l.render (editor.js:2) at d._renderValidatedComponentWithoutOwnerOrContext (editor.js:2)

Mabloq avatar Nov 29 '17 22:11 Mabloq

I got it working so that unresolved promises aren't an issue, but the ref defined in the EmailEditor component's ref will often times return undefined until I reload the page again, a clue!

My refs.editor is sometimes undefined, honestly I'm not so certain why refs are sometimes defined sometimes not defined. In the end this is not an issue with the react-email-editor component its an issue with me not knowing how refs and component life cycle truley

I resolved to use this ducktape solution but I know setTimeOut functions will be bad for people with slow connections. I will close the issue know but I appreciate if anyone can help me with a better solution

loadDesign = () => {
    if (this.editor !== undefined) {
      this.editor.loadDesign (this.props.template);
    } else {
      setTimeout (() => this.editor.loadDesign (this.props.template), 3000);
    }

Mabloq avatar Nov 30 '17 15:11 Mabloq

For those struggling in 2023, no need for a timeout, just put the function that loads the design in onReady instead of onLoad since the ref will be loaded after everything is ready.

mersimoski avatar Jan 25 '23 15:01 mersimoski

@mersimoski , I need to load an html onReady function ,but getting these two errors everytime 1.editor.js:2 TypeError: Cannot read properties of undefined (reading 'values') 2.editor.js:2 Uncaught TypeError: Cannot read properties of undefined (reading 'values')

below is my on ready function

const onReady = () => { let html_data = JSON.parse( JSON.stringify(props.templateData.resource[0].welcome_contenthtml) ); const templateJson = { html_data }; emailEditorRef.current.editor.loadDesign(templateJson); }; ANY LUCK ?

udayan46 avatar Jul 05 '23 11:07 udayan46

@mersimoski , I need to load an html onReady function ,but getting these two errors everytime 1.editor.js:2 TypeError: Cannot read properties of undefined (reading 'values') 2.editor.js:2 Uncaught TypeError: Cannot read properties of undefined (reading 'values')

below is my on ready function

const onReady = () => { let html_data = JSON.parse( JSON.stringify(props.templateData.resource[0].welcome_contenthtml) ); const templateJson = { html_data }; emailEditorRef.current.editor.loadDesign(templateJson); }; ANY LUCK ?

Hey there, facing same issue. Did you get any solution for the same?

Anubhavgarg1910 avatar Apr 07 '24 05:04 Anubhavgarg1910