Skip to content

Commit

Permalink
feat: Added AbortController to ItemAutoList
Browse files Browse the repository at this point in the history
  • Loading branch information
Sebastian-Webster committed Feb 16, 2024
1 parent 894c104 commit 20f126e
Showing 1 changed file with 9 additions and 3 deletions.
12 changes: 9 additions & 3 deletions components/ItemAutoList.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { useContext, useEffect } from "react";
import { useContext, useEffect, useRef } from "react";
import { FlatList, RefreshControl, TouchableOpacity } from "react-native";
import { View, Text } from "react-native";
import { useTheme } from "@react-navigation/native";
Expand All @@ -10,7 +10,8 @@ import Ionicons from 'react-native-vector-icons/Ionicons';

export default function ItemAutoList({noItemsFoundText, centreIfNoItems, url, extraPOSTData = {}, DisplayComponent, extraProps = {}, state, dispatch, noMoreItemsText}) {
const {colors} = useTheme();
const {serverUrl} = useContext(ServerUrlContext)
const {serverUrl} = useContext(ServerUrlContext);
const AbortControllerRef = useRef(new AbortController());

function loadItems(reload) {
if ((!state.noMoreItems || reload) && !state.loading && !state.reloading) {
Expand All @@ -22,7 +23,7 @@ export default function ItemAutoList({noItemsFoundText, centreIfNoItems, url, ex
...extraPOSTData
}

axios.post(urlToUse, toSend).then(response => {
axios.post(urlToUse, toSend, {signal: AbortControllerRef.current.signal}).then(response => {
const {items, noMoreItems} = response.data.data;

dispatch({type: 'addItems', items, noMoreItems})
Expand All @@ -36,6 +37,11 @@ export default function ItemAutoList({noItemsFoundText, centreIfNoItems, url, ex

useEffect(() => {
loadItems()

return () => {
console.warn('Aborting network requests from ItemAutoList as component is getting unmounted.')
AbortControllerRef.current.abort();
}
}, [dispatch])

return (
Expand Down

0 comments on commit 20f126e

Please sign in to comment.