Skip to content

Commit

Permalink
Merge pull request #399 from SquareTable/397-add-abortcontroller-to-i…
Browse files Browse the repository at this point in the history
…temautolist

feat: Added AbortController to ItemAutoList
  • Loading branch information
Sebastian-Webster authored Feb 16, 2024
2 parents 894c104 + 20f126e commit faea8f8
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 faea8f8

Please sign in to comment.