You should not use the component method outside the component itself (or passing it as a callback to a child component). You should consider them as private methods.
However, you can use a React function called statics to provide functions that are accessible from outside the component. However, they should be considered as static functions of the class, and as a result, they do not gain access to the internal components of your component (for example, this.props or this.state ).
Here is an example of some statics setting for a component:
var Component = React.createClass({ statics: { // these functions are available outside the component renderToBody: function() { React.renderComponent( <Component />, document.body ); } }, // cannot access this function outside the component getHelloWorld: function() { return 'Hello World!'; }, render: function() { return ( <div>{this.getHelloWorld()}</div> ); } });
So, if we call React.renderComponent(Component({}), elem) , then the component will display on elem , but due to the static you can call Component.renderToBody() and it will display the component directly in the <body> element .
IE: Functions defined inside the statics component are available directly as static functions. However, you must remember that they are static in the sense that they are not part of the object instance, they are just functions that you can call in the class.
The whole idea of ββresponse is that the components are as self-sufficient as possible. You will never have to access the component instance function directly from outside the component, because instead you just change the details for the component and re-visualize it so that inside it can change.
Here is an example of this:
var Component = React.createClass({ propTypes: { // always get in the habbit of using propTypes! message: React.PropTypes.string.isRequired, show: React.PropTypes.bool }, render: function() { return ( <div style={{display: this.props.show ? 'block' : 'none'}}> {this.props.message} </div> ); } });
While you could create a show() method instead (so you can do component.show(true) or component.show(false) to show / hide), you will pass it as a property for the same result instead .
Calling React.renderComponent(Component({message: 'foo', show: true}), elem) will make the component visible, re-rendering with show: false will hide it, etc. The same result, but with the help of props, which is a reaction.