I'm learning React and I have ESLint installed in my project. It started giving me errors like:
Use callback in setState when referencing the previous state react/no-access-state-in-setstate
In my React component I have a constructor:
constructor() {
super()
this.state = {
currentIdVehicle: null,
currentIdModel: null,
currentIdVehicleFinal: null,
marca: [],
veiculo: [],
modeloEAno: [],
vehicleFinal: [],
infoTable: [],
};
}
In my function I have:
getMarca = () => {
this.setState({ marca: [], veiculo: [], modeloEAno: [] });
api.get(`/marcas.json`).then(res => {
res.data.map((item) => {
const joined = this.state.marca.concat([
{ name: item.name, id: item.id },
]);
this.setState({ marca: joined });
});
});
};
I understand that it is not correct to use this.state
within setState
, but how can I resolve this error?
The setState function takes an optional callback parameter that can be used to make updates after the state is changed. This function will get called once the state has been updated, and the callback will receive the updated value of the state.
Yes, you can call setState() within the callback of another setState() .
The second argument that can optionally be passed to setState is a callback function which gets called immediately after the setState is completed and the components get re-rendered.
The setState() schedule changes to the component's state object and tells React that the component and its children must rerender with the updated state: // Correct this.
Instead of this:
res.data.map((item) => {
const joined = this.state.marca.concat([
{ name: item.name, id: item.id },
]);
this.setState({ marca: joined });
});
Do this:
res.data.map((item) => this.setState(prevState => {
const joined = prevState.marca.concat([
{ name: item.name, id: item.id },
]);
return({ marca: joined });
}));
You're not supposed to directly or indirectly reference this.state
in what you eventually pass to this.setState
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With