I want to include external JavaScript library in my ReactJS app. I am using Topol.io plugin that loads email template inside ReactJS. however, i am not i am finding difficulties in Setting state in innerHtml script. i am using:
`componentWillMount() {
const loadScript = callback => {
const existingScript = document.getElementById("scriptPlugin");
if (!existingScript) {
const scriptPlugin = document.createElement("script");
scriptPlugin.id = "scriptPlugin";
scriptPlugin.innerHTML = `var TOPOL_OPTIONS = {
id: "#app",
authorize: {
apiKey: "xyz",
userId: "xyz"
},
templateId: "${this.state.TemplateId}",
removeTopBar: false,
callbacks: {
onSaveAndClose: function(json, html) {
// HTML of the email
console.log(html);
// JSON object of the email
console.log(json);
},
onSave: function(json, html) {
console.log(json);
console.log(html);
var encodedeString = window.btoa(JSON.stringify(json));
// HTML of the email
console.log(encodedeString);
// JSON object of the email
console.log(window.atob(encodedeString));
},
onAutoSave($json) {
console.log("Auto Save");
"${this.setState({ demo: `{json}` })}"
console.log(json);
}
}
};`;
document.body.appendChild(scriptPlugin);
const scriptLoader = document.createElement("script");
scriptLoader.type = "text/javascript";
scriptLoader.id = "scriptLoader";
scriptLoader.src =
"https://d5aoblv5p04cg.cloudfront.net/editor/loader/build.js";
document.head.appendChild(scriptLoader);
scriptLoader.onload = function() {
const scriptInit = document.createElement("script");
scriptInit.id = "scriptInit";
scriptInit.innerHTML = `TopolPlugin.init(window.TOPOL_OPTIONS);`;
document.body.appendChild(scriptInit);
};
this.setState({ loaded: true });
scriptPlugin.onload = () => {
// if (callback) callback();
};
}
};
loadScript(() => {});
}
`
So, i want to set the state using the variable inside or the value return by OnSave or AutoSave method.