3

lets say i have a testfunction written in reactjs just before the rendering, in the index.js file

function testfunction(){
console.log("this is test function written in reactjs")
}

and i want to call the above reactjs function in html script.

<html>
<body>

//including reactjs minified js in the index.html
<script src="reactjs.min.js"></script>

<script>
//here i want to call function written in reactjs file
testfunction();
</script>

</body>
</html>
1
  • only way to make it global or use IIFE Commented Mar 20, 2018 at 5:08

1 Answer 1

2

It's ugly and bad practice, but you can set a function to global (window) context so that other scripts can call it:

window.testfunction = () => console.log("this is test function written in reactjs");

Then, other scripts on the page, having access to window, will be able to call it.

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks man, it worked. if i use this in production, what would be the side effects of using this as global function?
It pollutes the global namespace and will make larger applications more difficult to refactor and debug later. It would be better to integrate your page script with your react script, that way they have access to the same scope.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.