Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

accessing variable from script tag in html file in react component [closed]

I've only started working on javascript and React.js recently. I currently have a few script tags in my main.html file, and I need to be able to access some variables from these tags in my React.js components. Is there anyway to do this?

like image 569
Bryant Khoo Avatar asked Jul 01 '16 03:07

Bryant Khoo


1 Answers

If you have defined the variables directly in your <script> tag, then they are global and you should be able to simply reference them from any of your React components.

That being said, you should not do that. This introduces a dependency that not only makes it very hard to reuse the component, but will also be a maintenance nightmare due to it's in-transparency.

Instead, when you render your root component, pass the variable as a property and pass it on through your component tree to the component that requires it. This way, only your root component depends on outside variables and the interfaces of your lower-level components are cleanly and transparently defined.

like image 140
TimoStaudinger Avatar answered Sep 22 '22 20:09

TimoStaudinger