An open API service indexing awesome lists of open source software.

https://github.com/mosesesan/mesan-react-native-resource-manager

This module provides a trio of powerful and reusable React hooks that help manage fetching, state, and CRUD operations for remote data.
https://github.com/mosesesan/mesan-react-native-resource-manager

Last synced: 3 months ago
JSON representation

This module provides a trio of powerful and reusable React hooks that help manage fetching, state, and CRUD operations for remote data.

Awesome Lists containing this project

README

          

# ๐Ÿ“ฆ React Native Resource Manager

A reusable set of React hooks to manage **remote data fetching** and **CRUD operations** in a clean, state-synchronized way.

This package provides a unified API to:
- Fetch paginated data from APIs
- Manage local and remote create/update/delete operations
- Normalize and extract data from API responses
- Handle loading, error, and pagination states

---

## ๐Ÿš€ Installation

Add the hooks folder to your React Native or React project and import from:

```ts
import { useResourceManager } from '@/hooks/resource-manager-hooks';

# ๐Ÿงฐ Resource Manager Hooks

This package contains a set of composable React hooks to simplify **fetching**, **mutation**, and **state management** of remote resources. It enables consistent and reusable handling of async data interactions in React (and React Native) apps.

## ๐Ÿ” Overview

### Hooks Included:

- **`useFetch`** โ€“ Fetch remote data with pagination, loading, and error state management
- **`useMutation`** โ€“ Handle local and remote Create, Update, and Delete operations
- **useResourceManager** โ€“ Combines `useFetch` and `useMutation` for full data lifecycle management

---

## ๐Ÿš€ Quick Start

### 1. Installation

This is a local package, so simply import from your project like:

```ts
import { useResourceManager } from '@/hooks/resourceManager';
```

Make sure your folder structure includes:

```
resourceManager/
โ”œโ”€โ”€ useFetch.ts
โ”œโ”€โ”€ useMutation.ts
โ”œโ”€โ”€ useResourceManager.ts
โ”œโ”€โ”€ resourceManager.types.ts
โ””โ”€โ”€ index.ts
```

### 2. Example Usage

```ts
const { state, queries, crud } = useResourceManager({
serviceFn: fetchUsers,
dataExtractor: (res) => ({
data: res.users,
totalResults: res.totalCount,
currentPage: res.page,
totalPages: res.totalPages,
}),
remote: {
insertFn: createUser,
updateFn: updateUser,
deleteFn: deleteUser,
},
});

useEffect(() => {
queries.fetchData();
}, []);
```

### UI Rendering Example

```tsx
(
crud.deleteItem(item.id)} />
)}
/>
```
---

## ๐Ÿ“ฆ Hook APIs

### `useFetch({ serviceFn, dataExtractor, onError, shouldFetch })`

Returns `[state, queries, mutations]`

- `state`: data, loading states, errors, pagination
- `queries`: fetchData, fetchNextPage, refetchData
- `mutations`: addNewData, updateExistingData, deleteExistingData, etc.
---

### `useMutation({ local, remote })`

Returns: `addItem`, `updateItem`, `deleteItem` and mutation status flags
---

### `useResourceManager({ serviceFn, dataExtractor, remote, onError, shouldFetch })`

Returns:
- `state`: from `useFetch`
- `queries`: fetch-related functions
- `crud`: mutation functions from `useMutation`
---

## ๐Ÿ›  Advanced Configuration

You can override:

- `dataExtractor` to customize response transformation
- `dataKey` in mutations to extract nested created/updated records
- `shouldFetch` to delay or prevent automatic fetching
---

## ๐Ÿงผ Best Practices

- Keep `serviceFn` and `remote` functions stable (use `useCallback` if needed)
- Provide `dataExtractor` to handle complex API response formats
- Combine with context providers if global state is needed

---

## ๐Ÿ“ Type Definitions

Types are declared in `resourceManager.types.ts` and exported for use in components or higher-order abstractions.

---

## ๐Ÿ“ƒ License

This is part of the DropIt project. Internal use only.