A lightweight and flexible React Native hook for managing API data fetching with pagination, error handling, refreshing, and data mutation capabilities.
- Simplifies API integration with built-in pagination and infinite scrolling.
- Handles loading, refreshing, and error states out of the box.
- Supports flexible data mutation methods: add, update, and delete.
- Easily customizable with pluggable service functions and data extractors.
- Compatible with any REST API or paginated data source.
Install the package using npm or yarn:
npm install react-native-use-fetch
or
yarn add react-native-use-fetch
The useFetch
hook returns an array with three items: state
, queries
, and mutations
. Use these to manage data fetching, query actions, and data mutations respectively.
import React, { useEffect } from 'react';
import { View, Text, Button, FlatList, ActivityIndicator } from 'react-native';
import useFetch from 'react-native-use-fetch';
const fetchUsers = async ({ page }) => {
const response = await fetch(`https://api.example.com/users?page=${page}`);
return response.json();
};
const extractUserData = (response) => ({
data: response.users,
totalResults: response.total,
currentPage: response.page,
totalPages: response.total_pages,
});
const UsersScreen = () => {
const [state, queries, mutations] = useFetch({
serviceFn: fetchUsers,
dataExtractor: extractUserData,
});
useEffect(() => {
queries.fetchData();
}, []);
const handleAddUser = () => {
const newUser = { id: Date.now(), name: 'New User' };
mutations.addNewData(newUser);
};
const handleUpdateUser = (id) => {
mutations.updateExistingData(id, { name: 'Updated User' });
};
return (
<View style={{ flex: 1, padding: 16 }}>
<Button title="Add User" onPress={handleAddUser} />
{state.isFetching && <ActivityIndicator size="large" />}
<FlatList
data={state.data}
keyExtractor={(item) => item.id.toString()}
renderItem={({ item }) => (
<View style={{ marginBottom: 10 }}>
<Text>{item.name}</Text>
<Button title="Update" onPress={() => handleUpdateUser(item.id)} />
<Button
title="Delete"
onPress={() => mutations.deleteExistingData(item.id)}
/>
</View>
)}
onEndReached={queries.fetchNextPage}
onRefresh={queries.refetchData}
refreshing={state.isRefreshing}
/>
{state.error && <Text style={{ color: 'red' }}>Error: {state.error}</Text>}
</View>
);
};
export default UsersScreen;
This hook manages API data fetching, pagination, refreshing, error handling, and data mutations.
options
(optional): Configuration object with the following properties:initialData
(array): Initial data to populate.serviceFn
(function): Async function to fetch data, receives{ page: number }
.dataExtractor
(function): Function to extract data and pagination info from the response.onError
(function): Callback for error handling.shouldFetch
(boolean): Whether to fetch data immediately on mount (defaulttrue
).
-
state
(object): Includes both state values and setter functions.- State:
data
(array)error
(string ornull
)page
(number)hasNextPage
(boolean)totalResults
(number ornull
)isFetching
(boolean)isRefreshing
(boolean)isFetchingMore
(boolean)
- Setters:
setData(data: any[])
setError(error: string | null)
setPage(page: number)
setHasNextPage(boolean)
setTotalResults(number | null)
setIsFetching(boolean)
setIsRefreshing(boolean)
setIsFetchingMore(boolean)
- State:
-
queries
(object):fetchData(refresh?: boolean, page?: number, more?: boolean)
refetchData()
fetchNextPage()
-
mutations
(object):addNewData(newData)
updateExistingData(id, newData)
updateExistingDataWithKey(matchId, keyToUpdate, newValue, matchKey?)
deleteExistingData(id)
- On mount, the hook optionally fetches data from an API using a user-provided
serviceFn
. - The
dataExtractor
processes and structures the API response, including pagination. - Fetch state is split between querying (
fetchData
,refetchData
,fetchNextPage
) and mutation (addNewData
,updateExistingData
, etc.) actions. - Built-in setters (e.g.,
setData
,setError
, etc.) allow manual control over hook state. - Mutation methods offer convenient, immutable updates to the data array.
- Pagination and infinite scroll are supported automatically using
hasNextPage
and page tracking.
Contributions are welcome! If you have improvements, bug fixes, or ideas, feel free to fork the repo and submit a pull request. Be sure to include tests and follow the coding style used throughout the project. Discuss features or bugs via GitHub Issues.
Need help or found a bug? Open an issue on the GitHub Issues page. We're happy to help!
Licensed under the MIT License. See the LICENSE file for full details.
Created by Moses Esan.