I don't even know the name for this, hence the title.
I basically initialize my modal form by either loading the data passed via props (for editing) or loading an empty struct (to create new entries):
defaultStruct: {
text: null,
active: true,
options: [
{
text: null,
correct: true,
position: 0,
_destroy: false,
}
]
},
getInitialState() {
return {
question: this.props.initialQuestion == null ? this.defaultStruct : this.props.initialQuestion,
};
}
The main idea is to reset the form using this.defaultStruct again, after submitting the form.
But, when I do a console.log(this.defaultStruct) after submit, I can see that it has actually been populated with data from the form's state. So... what the hell.
handleSubmit(e){
if(e){e.preventDefault();}
$.ajax({
url: this.state.question.id == null ? this.props.applicationData.routes.questionsCreate : this.props.applicationData.routes.questionsUpdate,
dataType: 'json',
contentType: 'application/json',
type: 'POST',
data: this.digestDataForStupidRails('question', 'options'),
success: function(data) {
console.log(this.defaultStruct)
}.bind(this),
error: function(xhr, status, err) {
}.bind(this)
});
}
Console output:
{
"question": {
"text": "preg 1",
"active": true,
"options": [
{
"text": "preg 1 a 1",
"correct": false,
"position": 0,
"_destroy": false
},
{
"text": "preg 1 a 2",
"correct": true,
"position": 1,
"_destroy": false
}
]
}
}
Of course I can solve this by using a function instead, and make it return an empty struct each time, but this keeps me curious nevertheless.
Can anybody explain this behavior?