Friday, May 17, 2024
 Popular · Latest · Hot · Upcoming
11
rated 0 times [  18] [ 7]  / answers: 1 / hits: 98167  / 8 Years ago, fri, january 29, 2016, 12:00:00

Im not sure if this has been asked before or anybody has encountered the same issue on reactjs. So the scenario is like this, I have an index.html file that includes some javascript. Now on my react component, I have a condition that will render only if the condition is true. This means that initially when my page loaded the component has not been rendered yet. When I toggle a button this is where that component gets rendered. That child component needs to call a javascript method that was included on my index.html. How should I do this?



Any help is greatly appreciated.


More From » reactjs

 Answers
51

In index.html


<script type="text/javascript">
function test(){
alert('Function from index.html');
}
</script>

In your component


componentWillMount() {
window.test();
}

2022 Typescript edit:


Create a file global.d.ts like so (doc):


interface Window {
test: () => void;
}


[#63516] Wednesday, January 27, 2016, 8 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
katelinb

Total Points: 535
Total Questions: 104
Total Answers: 109

Location: Burkina Faso
Member since Sun, Jun 13, 2021
3 Years ago
;