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?