I need to construct some script tags that has variables with params that comes back from an API response. The variables needs to be available in the HTML so it can be accessed after page load. One example of a script needed would be something like this.
<script type="text/javascript">var test_var = "test123";</script>
The current implementation that I have is this
useEffect(() => {
const script = document.createElement("script");
script.type = "text/javascript";
script.innerHTML = `var test_var = ${testId};`;
document.body.appendChild(script);
return () => {
document.body.removeChild(script);
}
}, []);
By doing it that way will the variable be accessible in the HTML of the page since I am technically just setting the whole thing as a string in the script.innerHTML? Upon inspect element it pretty much just shows as
<script type="text/javascript">var test_var = test123;</script>