Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Running unlayer.init in React #385

Open
brennanleez-coder opened this issue Mar 2, 2024 · 3 comments
Open

Running unlayer.init in React #385

brennanleez-coder opened this issue Mar 2, 2024 · 3 comments

Comments

@brennanleez-coder
Copy link

Hi everyone, where do i run the unlayer.init method.

I am trying to get the editor to load from a template hence I need this setup.

@SouravBandyopadhyay
Copy link

As working in React, unlayer means => emailEditorRef.current.editor (ref hook)
Document Link: https://docs.unlayer.com/docs/react-component

import React, { useRef } from 'react';
import EmailEditor from 'react-email-editor';

const App = () => {
  const emailEditorRef = useRef(null);

  const exportHtml = () => {
    emailEditorRef.current.editor.exportHtml((data) => {
      const { html } = data;
      console.log('exportHtml', html);
    });
  };

  const onReady = () => {
    console.log('Editor is ready');
    // Load your template here
    const templateJson = {}; // Your Template JSON
    emailEditorRef.current.editor.loadDesign(templateJson);
  };

  return (
    <div>
      <div>
        <button onClick={exportHtml}>Export HTML</button>
      </div>
      
      <EmailEditor
        ref={emailEditorRef}
        onReady={onReady}
      />
    </div>
  );
};
export default App;

@paulgraveseca
Copy link

That approach doesn't work for calling the init function.

The init function doesn't exist on emailEditorRef.current.editor. I need to call init to set up things like mergeTags, how do I do this in React?

@SouravBandyopadhyay
Copy link

For merge tag you can either hardcode it inside Editor components in options or while initializing the editor connect it with some kind of dataset to load it.
<EmailEditor ref={emailEditorRef} onReady={onReady} options={{ mergeTags: { first_name: { name: "First Name", value: "{{first_name}}", }, } }} />

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants