-
Notifications
You must be signed in to change notification settings - Fork 752
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
side effects of onLoad implementation #22
Comments
@HyperMaxime can you share a functional code sample of your particular scenario? If not, then any snippets would help too. |
@umairsiddique I also get the same issue from time to time. My onLoad function:
Where I use the onLoad:
Error it spits out: |
@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 Also notice that sometimes it is actually available at the start, depending on the browser having cached the unlayer script. |
@umairsiddique @HyperMaxime sorry to be a pest, but did either of you ever find a solution or work around to this issue? Cheers. |
@stroypet until we get a better solution/fix we are just pointing to the global variable unlayer. |
@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 :). |
@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')) |
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! )) |
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):
If anyone needs additional clarity, please respond to this comment. I am willing to share more code. |
@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, |
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: Implement the hook: Implement the onLoad: Then a function to be called and check if we are able to use the editor: The render: I hope this can help. |
This solution works for me #7 (comment) |
@mvcarvalho This worked for me when trying to use |
@mvcarvalho Works for me, thank you! |
in case it helps anyone, I have posted up a working solution at #100 (comment) |
The passed wrapper prop
onLoad
function gets called in direct response (synchronous) to theonLoad
callback of the script being loaded. This does not always guarantee that the underlying component is already loaded by then.For example the editor can be in specific route of a react application, and load the unlayer script the first time the user gets there. If the user then navigates away from that route and comes back, the script will be loaded from the cache, therefore immediately kicking in the
onLoad
callback.In the demo, the
onLoad
function will executethis.editor.loadDesign(sample)
and in my particular case add event listeners. It then blows up sayingthis.editor
is undefined.The text was updated successfully, but these errors were encountered: