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?
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With