React.js ReactComponent does not offer setState ()?

I seem to misunderstand some of the fundamental parts of React.js.

In http://facebook.imtqy.com/react/docs/component-api.html

he says the responsive component has methods like setState ().

But when I do this:

var MyComp = React.createClass({ getInitialState: function() { return {dummy: "hello"}; }, render: function() { return React.DOM.h1(null, this.state.dummy + ", world!") } } var newComp = MyComp(null); React.renderComponent(newComp, myDomElement); MyComp.setState({dummy: "Good Bye"}); // Doesn't work. setState does not exist newComp.setState({dummy: "Good Bye"}); // Doesn't work either. setState does not exist 

There is no setState () method. But in the docs, he says component APIs, so am I wrong here?

+8
javascript reactjs
source share
1 answer

According to this blog post and this entry , the call to MyComp no longer returns an instance, it returns a light handle.

Anti-pattern:

 var MyComponent = React.createClass({ customMethod: function() { return this.props.foo === 'bar'; }, render: function() { } }); var component = <MyComponent foo="bar" />; component.customMethod(); // invalid use! 

Proper use:

 var MyComponent = React.createClass({ customMethod: function() { return this.props.foo === 'bar'; }, render: function() { } }); var realInstance = React.renderComponent(<MyComponent foo="bar" />, root); realInstance.customMethod(); // this is valid 
+12
source share

All Articles