I have an app that read values from an external devices, then these values are written in a DB. When I wrote this data in the db I receive this error:
Can't perform a React state update on an unmounted component. This is a no-op, but it indicates a memory leak in your application. To fix, cancel all subscriptions and asynchronous tasks in the componentWillUnmount method.
And It is about this code:
this.setState(state => ({
acc_dx,
array_acc_dx: [...state.array_acc_dx, [timeagm, ...acc_dx].join(":")]
}));
this code is a part of a function setupNotifications1 which in turn is called in a function called in the componentDidMount ().
How can I solve this warning? Thank you.
EDIT 1:
componentDidMount() {
this.deviceService1(this.device1);
}
Inside deviceService1()
I call this.deviceService2(this.device2)
and inside I call setupNotifications1(this.device1)
and setupNotifications2(this.device2)
EDIT 2:
async setupNotifications1(device) {
var timeagm = 0;
var time = 0;
const service = this.serviceGeneral();
this.subscriptionMonitor1 = await device.monitorCharacteristicForService(
service,
this.AccGyrMg,
(error, characteristic) => {
if (error) {
this.error(error.message);
return;
}
const buf = Buffer.from(characteristic.value, "base64");
const [...acc_dx] = [2, 4, 6].map(index => buf.readInt16LE(index));
this.setState(state => ({
acc_dx,
array_acc_dx: [...state.array_acc_dx, [timeagm, ...acc_dx].join(":")]
}));
timeagm += 20;
then I have a stop button that is used to remove this.subscriptionMonitor1 and change page ( I pass the value in the other page where there are written in the DB )