1

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.

4
  • 1
    is it possible to call function from reactcomponents in javascript script? Yes. That is completely possible. Create a module and import necessary component / function and call it Commented Mar 7, 2019 at 9:03
  • 1
    Of course, React is just a library for javascript. You can call builtin javascript function as well as user-defined global javascript function from your react code Commented Mar 7, 2019 at 9:04
  • any example or something? I'm working first time with this combination so please send me some example. Commented Mar 7, 2019 at 9:04
  • 1
    StackOverflow is not a code-writing service. Please read through the Help Center, in particular How do I ask a good question? Commented Mar 7, 2019 at 9:08

1 Answer 1

5

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

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

Comments

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.