Skip to content Skip to sidebar Skip to footer

Call React Component's Function From External Javascript Function

I am trying called the react component's function from a javascript function. On the initial stage, I want to call my function which is declared in react component but rather than

Solution 1:

You can can assign a function to the window object as a function and call that from your script. So essentially you would do the following:

componentWillMount() {
  window.reactFunction = () => {
    console.log('Test');
  }
}

Then call it from your script like so:

<scripttype="text/javascript">functiontest(){
    window.reactFunction()
  }
</script>

Admittedly I have no idea how exactly you plan to use this but this could be one way of accomplishing what you want by utilizing the global window object. The second part would realistically need some way of determining whether the react component has mounted (in a non trivial example), or just check for the existence of the method before calling it.

Post a Comment for "Call React Component's Function From External Javascript Function"