也许我认为可以解决我的问题的方法不是正确的。很高兴听到你的想法。我得到了:
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 async task in a useEffect cleanup function
并追踪到一个组件,该组件位于状态栏的headerRight部分。我的印象是它只会安装一次。无论如何,组件都会与发生并更新状态的同步进程进行通信。对于每个同步状态,将显示不同的图标。
dataOperations是一个NativeModules类,它与一些执行后台同步并将状态发送给RN的JAVA进行对话。
import React, {useState, useEffect} from 'react';
import {DeviceEventEmitter } from 'react-native';
import DataOperations from "../../../../lib/databaseOperations"
const CommStatus: () => React$Node = () => {
let [status, updateStatus] = useState('');
const db = new DataOperations();
const onCommStatus = (event) => {
status = event['status'];
updateStatus(status);
};
const startSyncing = () => {
db.startSyncing();
};
const listner = DeviceEventEmitter.addListener(
'syncStatusChanged',
onCommStatus,
);
//NOT SURE THIS AS AN EFFECT
const removeListner = () =>{
DeviceEventEmitter.removeListener(listner)
}
//REMOVING THIS useEffect hides the error
useEffect(() => {
startSyncing();
return ()=>removeListner(); // just added this to try
}, []);
//TODO: find icons for stopped and idle. And perhaps animate BUSY?
const renderIcon = (status) => {
//STOPPED and IDLE are same here.
if (status == 'BUSY') {
return (
<Icon
name="trending-down"
/>
);
} else if (status == 'IS_CONNECTING') {
...another icon
}
};
renderIcon();
return <>{renderIcon(status)}</>;
};
export default CommStatus;
组件作为堆栈导航的一部分加载,如下所示:
headerRight: () => (
<>
<CommStatus/>
</>
),
发布于 2020-05-10 23:20:49
为此,您可以使用App.js。
<Provider store={store}>
<ParentView>
<View style={{ flex: 1 }}>
<AppNavigator />
<AppToast />
</View>
</ParentView>
</Provider>
因此,在这种情况下,将只挂载一次。
https://stackoverflow.com/questions/61713078
复制相似问题