I am new to react.
I want to add another SingleButtonComponent when a SingleButtonComponent is clicked. I am able to increment the number of children using a state variable numChildren.
AddChild: function() {
var numChildren = (this.state.numChildren) +1;
this.setState({numChildren :numChildren})
},
But I am getting error whenever I am trying to loop inside
render: function () {
return (
for (var i = 0; i < this.state.numChildren; i += 1) {
<SingleButtonComponent AddChild={this.AddChild}/>
};
)
}
I have temporarily kept the for loop out of render..
Please suggest me a way to add a child-component every time it is clicked.
Thanks
Your
render
function doesn't return anything.If you're using React 16, try:
Otherwise, you must return a single element:
Regarding your second question:
React.createClass
is deprecated in favor of ES6 classes. Your component should be defined as follows: