这是一个简单计数器的代码。
但是,当我渲染视图时,我没有任何输出。请告诉我代码有什么问题。
按下按钮,计数器增加,并显示在屏幕上。
var Title = React.createClass({ getInitialState : function(){ return {count:0}; }, increment : function(){ this.setState({count:this.state.count+this.props.incVal}); }, render: function() { return ( <div> <h1 >Count : {this.state.count}< /h1> <button onClick={this.increment} >Increment</button> </div> ); } }); var MultiButton = React.createClass({ render : function (){ return( <Title incVal={1}/> <Title incVal={5}/> ); } }); ReactDOM.render( <MultiButton /> , document.getElementById('example') );
您不能从React类返回多个元素。如果您有多个元素,则将它们包装在一个div中,例如
var MultiButton = React.createClass({ render : function (){ return( <div> <Title incVal={1}/> <Title incVal={5}/> </div> ); } });