Answer the question
In order to leave comments, you need to log in
Where to store the current form value?
Hello. Actually I write on reactJs + redux.
There are forms, and now, when information changes there, I store the changing information in the state of this particular component, and when the save button is pressed, I send it to the database and, accordingly, to the state of the entire application.
The question arose, is it worth declaring these current state in the state of the entire application and changing them there? How appropriate, optimized, and so on. What is considered best practices here?
update.
I'll update the question with a link to an example.
And some code
state = {
currentDepartment: {
name: ''
}
}
// make request to DB
componentWillMount() {
this.props.fetchDepartments();
this.props.fetchDepartment(+this.props.params.departmentId);
}
// set current state value from DB
componentWillReceiveProps(nextProps) {
this.setState({
currentDepartment: {
name: nextProps.singleDepartment.name
}
});
}
// change current state data on input change
handleInputChange = (evt) => {
const target = evt.target;
const value = target.value;
const name = target.name;
this.setState({
currentDepartment: {...this.state.currentDepartment, [name]: value}
});
}
// send updated data to DB
handleUpdate = (evt) => {
evt.preventDefault();
const previousDepartment = h.findById(this.props.singleDepartment.id, this.props.departments);
const updatedDepartment = h.updateItem(previousDepartment, this.state.currentDepartment);
this.props.saveDepartment(updatedDepartment);
}
Answer the question
In order to leave comments, you need to log in
I store changing information in the state of this particular component
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question