Skip to content

Commit

Permalink
fix: dont try and update unmounted component (#38)
Browse files Browse the repository at this point in the history
* fix: dont try and update unmounted network logs

* refactor: cleanup display logic
  • Loading branch information
chrisbruford authored Nov 4, 2020
1 parent 1506f03 commit 59af7e9
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 5 deletions.
22 changes: 17 additions & 5 deletions example/src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import {
Platform,
View,
Text,
Alert,
TouchableOpacity,
} from 'react-native';
import NetworkLogger, {
Expand Down Expand Up @@ -41,12 +40,23 @@ export default function App() {

const styles = themedStyles(theme === 'dark');

const goBack = () => {
Alert.alert('Going back');
};
const goBack = () => setUnmountNetworkLogger(true);

const [unmountNetworkLogger, setUnmountNetworkLogger] = useState(false);

const backHandler = getBackHandler(goBack);

const remountButton = (
<View>
<Button
title={'Re-open the network logger'}
onPress={() => setUnmountNetworkLogger(false)}
>
Re-open network logger
</Button>
</View>
);

return (
<SafeAreaView style={styles.container}>
<View style={styles.header}>
Expand All @@ -62,7 +72,9 @@ export default function App() {
</Text>
<View style={styles.navButton} />
</View>
<NetworkLogger theme={theme} />
{(unmountNetworkLogger && remountButton) || (
<NetworkLogger theme={theme} />
)}
<View style={styles.bottomView}>
<Button
title="Toggle Theme"
Expand Down
5 changes: 5 additions & 0 deletions src/components/NetworkLogger.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,11 @@ const NetworkLogger: React.FC<Props> = ({ theme = 'light', sort = 'desc' }) => {
});

logger.enableXHRInterception();

return () => {
// no-op if component is unmounted
logger.setCallback(() => {});
};
}, [sort]);

useEffect(() => {
Expand Down

0 comments on commit 59af7e9

Please sign in to comment.