I have two react classes both have render function that returns forms with a button. In class A, I had to import class B so i can use form from class B.
My problem: When I clicked the inner button, the outer button gets triggered as well but I only want the inner submit button to work.
My attempt: I checked the console and found out that it is giving me a warning: validateDOMNesting: form cannot appear as a descendant of form.
I did the research and thought that the problem may due to the nested form . So I found out a workaround from SO by changing the <form></form>
in the class B to <div></div>
, so it is no longer a form. Now it does not give me error on console, but the problem still remains: if I click the inner button, the outer button got triggered as well which invoked two functions in total.
Where I need help: I really want to figure out what cause this behavior(I am thinking maybe changing the tag does not change the nested form problem or maybe the way how I referencing the other class is not completely right)
My code:(Please notice that I simplified it)
var classA = React.createClass({
render: function() {
return (
<div>
<form
className={this.props.className}
id={this.props.controllerSingular + '_form'}
onSubmit={this.handleSubmit}
ref="some_form"
>
<FieldSet>
<ClassB id={this.state.id} />
</FieldSet>
<button onClick={this.submit} className="btn btn-primary">
Save
</button>
</form>
</div>
)
},
})
var classB = React.createClass({
render: function() {
return (
<div>
<button id="some_ID" className="some_name" onClick={this.handleRegAdd}>
Add
</button>
</div>
)
},
handleRegAdd: function() {
var that = this
var regLines = this.state.regulation
regLines.push({
reg_id: null,
Type: $('.reg-type-select span.Select-value-label').text(),
id: that.props.id,
Hours: null,
Minutes: null,
Days: '',
Start: null,
End: null,
})
this.setState({
regulations: regLines,
})
},
})
when I clicked the button from class B, which should only trigger function "handleRegAdd". However, it also triggered submit function in class A