I have created a react app using create-react-app my-app
. I have an existing web page, and the react app attaches to a div within the html page - as per normal.
The web page has some global javascript constants const1
and const2
that are needed for the react app to function. Is there a way I can pass these variables to the react app? The structure is as follows
<script type="text/javascript">
const const1 = "1234";
const const2 = "5678";
</script>
<script type="text/javascript" src = "/static/react-app.js" ></script>
I'm struggling because the javascript is of course minified during the build phase, so any declarations as follows:
class App extends React.Component{
constructor(props) {
super(props);
this.state = {
stat1: const1,
stat2: const2,
don't work as the variables are re-written. I have tried to be sneaky and use eval as follows
const const1 = eval("function c1(){console.log(const1);return const1;};c1();");
but the eval (and the console.log) return undefined. Is there a way I can invoke a react component, and pass it variables from the outside?