I got a obj in state and all by default are false:
obj: {
field1: false,
field2: false,
field3: false
}
I want to set all to true after submit, all are dynamic:
handleSubmit = (e) => {
e.preventDefault();
const doValid = {
field1: true,
field2: true,
field3: true,
}
//this is static, in real code it comes from validator and decide to be false or true
for (let i = 0; i < Object.keys(doValid).length; i++) {
this.handleValid(Object.keys(doValid)[i], Object.values(doValid)[i]);
}
}
And here is I set each state to true dynamically:
handleValid = (type, v) => {
this.setState({
...this.state,
obj: { ...this.state.obj,
[type]: [v]
}
})
}
As you see, I used [type] and [v] for this, but the problems:
- When I click on submit button, state not change to
true(I know maybe it's a delay to get changed states) (if I right ignore this one) - If I click again, now I see just last item changed to
true, but here is two problems:
a: Why just last item changed?
b: Why it changed like this? with bracket, that make it to array but it just a dynamic to decide it's true or false:
field1: false
field2: false
field3: [true]
It should be like this:
field1: true
field2: true
field3: true
(See console log please in jsfiddle)
