I am working on a small react project where I need to run a script whenever a certain component renders. The script is written in javascript and the filename is in the following format: .min.js
What I've tried so is,
componentDidMount() {
// Create a script in the DOM
const script = document.createElement('script');
// Assign properties to the script (src, id, data-brand, data-ref, asynv)
script.src =
'SRC_TO_MY_SCRIPT';
script.id = 'SCRIPT_ID';
script['data-brand'] = 'DATA_BRAND';
script['data-ref'] = 'DATA_REF';
script.async = true;
// Append the script into the body of the react app
this.div.appendChild(script);
}
and the div in returned in the render function of the class component the script is ought to run in. The thing is, I can see the script is loaded inside the div of the component but it never works. Is there any problem in the way I implement it?!
Thanks in advance.