Enda Enda - 1 year ago 87
React JSX Question

How to call a method in the React component in an external script

I would like to call in the external script, or console React components of the method

I tried to do so

window.mwap = this;

but no use

enter image description here

please help me

Answer Source

The return value of ReactDOM.render() is actually the mounted instance of the component:

class Counter extends React.Component {
  constructor(props) {
    this.state = {
      count: 0
  inc() {
    this.setState({count: this.state.count + 1});
  render() {
    return (
          Count: {this.state.count}

const instance = ReactDOM.render(
  <Counter />,

<script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react-dom.min.js"></script>

<div id="app"></div>

If you need access to a deeply nested component, that's a bit more difficult, but this should get you started.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download