Answer the question
In order to leave comments, you need to log in
How to properly handle react controlled component?
Hello everyone, could you tell me the best way to handle a controlled component in React?
There is a table with data, some of the cells of which contain changeable data, for example, inputs, checkboxes.
Now I create handlers for each cell being changed, passing the id of the line being edited to the handler, then I find this line in the state and change the data in it.
checkbox handler looks like this
limitSwitchHandler = rowId => {
this.setState(({data}) => {
const idx = data.findIndex(el => el.id === rowId);
const oldRow = data[idx];
const newRow = {...oldRow, autoLimit: !oldRow.autoLimit};
const newArray = [
...data.slice(0, idx),
newRow,
...data.slice(idx + 1),
];
return {
data: newArray,
}
});
};
<Switch
checked={element.autoLimit}
onChange={() => this.limitSwitchHandler(row.id)}
/>;
onChange={this.limitSwitchHandler}
limitPerDayHandler = (rowId) => (e) => {
e.persist();
console.log(e.target.value);
this.setState(({data}) => {
const idx = data.findIndex(el => el.id === rowId);
const oldRow = data[idx];
const newRow = {...oldRow, limitPerDay: e.target.value};
const newArray = [
...data.slice(0, idx),
newRow,
...data.slice(idx + 1),
];
return {
data: newArray,
}
});
};
<TextInput
name="limitPerDay"
value={element.limitPerDay}
onChange={this.limitPerDayHandler(element.id)}
/>;
Answer the question
In order to leave comments, you need to log in
Synthetic events cannot be used asynchronously, e.persist() releases the event from the pool and it is not cleared, so all values are preserved at the time the asynchronous call is made.
Your problem was solved like this:
In your solution, it is more correct to name the handler:
since technically the call is not a handler, but returns one.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question