Comments (16)
The key fix is to check if this.editor && window.unlayer != undefined and by conditionally setting the onLoad prop. In my particular use case, I am using react-router-dom and an api call that Redux dispatches when the componentDidMount. I figured out this fix (fyi this.props.match is a prop passed in from react-router-dom so ignore it if your component is not using it):
// Function outside render but inside class MyClassName extends Component { }
loadDesign = design => this.editor.loadDesign(design)
render() {
// Check if Redux updated the state from mapStateToProps
const design = this.state.HtmlDocument.hasOwnProperty('design') ? JSON.parse(this.state.HtmlDocument.design) : null
// True if there are paramaters in the url, redux updated the state, and if the editor has loaded into memory
const isEditingDesign = this.props.match && design && this.editor && window.unlayer
return (
<EmailEditor minHeight="85vh" ref={editor => this.editor = editor} style={styles} onLoad={isEditingDesign ? this.loadDesign(design) : null}/>
)
}
export default withRouter(reduxConnect(mapStateToProps, mapDispatchToProps)(MyClassName))
If anyone needs additional clarity, please respond to this comment. I am willing to share more code.
from react-email-editor.
@strap8, I did not test your code in practise so I might be wrong, but at first sight I think your solution misses the point of this issue. Under certain conditions, this.editor
will not yet be available when the onLoad
callback gets called, therefore throwing an exception if any method of it is being executed. Your solution simply doesn't run any callback in that scenario, which will indeed not throw any exception, but will also eventually not run code that is expected to run at the moment the editor is loaded.
from react-email-editor.
I'm using a workaround to fix this. I facing this problem when the route change, using react-router-dom.
So, to "solve" this, I'm using 2 flags: isEditorLoaded and isComponentMounted
Initiate them in the constructor:
constructor(props) { super(props); this.editor = null; this.isEditorLoaded = false; this.isComponentMounted = false; }
Implement the hook:
componentDidMount() { this.isComponentMounted = true; this.loadTemplate(); }
Implement the onLoad:
onLoad = () => { this.isEditorLoaded = true; this.loadTemplate(); }
Then a function to be called and check if we are able to use the editor:
loadTemplate = () => { if (!this.isEditorLoaded || !this.isComponentMounted) return; this.editor.loadDesign(this.state.jsonTemplate) }
The render:
<EmailEditor ref={editor => this.editor = editor} onLoad={this.onLoad} />
I hope this can help.
from react-email-editor.
@umairsiddique I created a CodeSandbox that illustrates the issue: https://codesandbox.io/s/5kz6nzjq9p
By showing and hiding the editor, you'll see in the log that the editor is undefined when the onLoad
callback function executes.
Also notice that sometimes it is actually available at the start, depending on the browser having cached the unlayer script.
from react-email-editor.
@umairsiddique I also get the same issue from time to time.
My onLoad function:
onLoad = () => {
this.editor.loadDesign(this.props.firm_prefs[1].email_templates[this.state.selectedTemplate]);
this.editor.setMergeTags([ {name: "First Name", value: "{*|first_name|*}"}, {name: "Last Name", value: "{*|last_name|*}"}, {name: "Invite Link", value: "{*|invite_link|*}"}]);
}
Where I use the onLoad:
<EmailEditor
onLoad={this.onLoad}
ref={editor => this.editor = editor}
/>
</div>
Error it spits out:
from react-email-editor.
@stroypet until we get a better solution/fix we are just pointing to the global variable unlayer.
from react-email-editor.
This solution works for me #7 (comment)
from react-email-editor.
@stroypet The usage example would become something like this:
import React, { Component } from 'react'
import { render } from 'react-dom'
import EmailEditor from 'react-email-editor'
class App extends Component {
render() {
return <div>
<h1>react-email-editor Demo</h1>
<div>
<button onClick={this.exportHtml}>Export HTML</button>
</div>
<EmailEditor />
</div>
}
exportHtml = () => {
unlayer.exportHtml(data => {
const { design, html } = data
console.log('exportHtml', html)
})
}
}
render(<App />, document.getElementById('app'))
from react-email-editor.
Solution with the global variable works. Just use window.unlayer. Was trying to fix it half a day, thank you all. It is not good to use global variables in React but who cares, IT WORKS! ))
from react-email-editor.
@HyperMaxime can you share a functional code sample of your particular scenario? If not, then any snippets would help too.
from react-email-editor.
@umairsiddique @HyperMaxime sorry to be a pest, but did either of you ever find a solution or work around to this issue?
Cheers.
from react-email-editor.
@coxom Thanks for the reply, don't suppose I could bother you for a code snippet of what that looks like? My .js skills aren't the greatest :).
from react-email-editor.
@mvcarvalho This worked for me when trying to use setMergeTags
. It feels like the best workaround so far. Have you managed to get it working in a different way since your post?
from react-email-editor.
@mvcarvalho Works for me, thank you!
from react-email-editor.
in case it helps anyone, I have posted up a working solution at #100 (comment)
from react-email-editor.
Sometimes the email editor shows up but most of the time on refreshing/reconnecting I get these errors in the browser console. The same thing happens in the case of "save Design" and "load design" both
- ERROR: (Cannot read property save Design/ load Design of Undefined)
- ERROR: (Prop
id
did not match. Server: "editor-2" Client: "editor-1")
sometimes even with ERROR2 things work fine.
WHAT SHOULD I DO......?
from react-email-editor.
Related Issues (20)
- Increase in input field length in exported HTML
- Embed Display Conditions Doesn't work properly for Outlook mobile.
- Background image is not responsive like another main image.
- Docs for Custom CSS - Available classes / variables to edit HOT 6
- Hide Drag and Drop
- Rich Text Editor doesn't work properly in custom tool.
- Adding custom loading spinner component HOT 2
- Usage and Save Post API request fails with 502 Request Code Bad Gateway HOT 4
- Running unlayer.init in React HOT 3
- Query regarding the APIs Auth Session triggered
- AccessDeniedAccess error HOT 10
- date field in form (Unlayer)
- image export failing
- List overflow causes vertical spacing between blocks
- Box on left and right side
- loading template in react-email-editor HOT 6
- How to add API-KEY in react-email-editor HOT 1
- how can disable a card like "HTML" and "menu" if we don't need it. IS there any option to disable cards HOT 1
- Renders twice with latest nextjs HOT 1
- "design:updated" event is not triggered when consecutive changes are made to the currently focused element
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from react-email-editor.