Devmasta
Devmasta

Reputation: 563

Call Javascript function from react components

I have one question because I'm not sure if that possible. I have ReactJS project that included some javascript functions.

I found solution to call javascript function from react components with window object but is it possible to call function from reactcomponents in javascript script?

For example I have definied function in React component. Is it possible to call that function in javascript?

Thank you.

Upvotes: 1

Views: 4826

Answers (1)

lumio
lumio

Reputation: 7575

Yes it is.

const App = () => <div>Hello world { externalFunction() }</div>;

ReactDOM.render( <App/>, document.querySelector( '#root' ) );
<script>
  // Imagine this is an external source
  function externalFunction() {
    return Math.round( Math.random() * 100 );
  }
</script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/16.6.3/umd/react.production.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react-dom/16.6.3/umd/react-dom.production.min.js"></script>

<div id="root"></div>

Resources

Upvotes: 5

Related Questions