# react-select-fetch

> Wrapper above react-select-async-paginate that loads options from specified url

Latest version **0.5.6** (published 2024-12-06) · MIT license · 0 weekly downloads

## Install

```sh
npm install react-select-fetch
pnpm add react-select-fetch
yarn add react-select-fetch
bun add react-select-fetch
```

## Health

**Score 45/100 (D)** — status: stable.

Positive: has types; esm support; no vulnerabilities.

Warnings: low downloads; pre 1.0.

Negative: stale.

## Facts

| | |
|---|---|
| Version | 0.5.6 |
| Published | 2024-12-06 |
| First published | 2020-02-02 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | ESM + CommonJS |
| Dependencies | 2 |
| Unpacked size | 42.3 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 331 |
| Author | Vadim Taits |
| Maintainers | vtaits |
| Keywords | react, react-select, async, pagination, fetch |

## Links

- npm: https://www.npmjs.com/package/react-select-fetch
- Repository: https://github.com/vtaits/react-select-async-paginate
- Homepage: https://github.com/vtaits/react-select-async-paginate/tree/master/packages/react-select-fetch
- Issues: https://github.com/vtaits/react-select-async-paginate/issues
- npm.io page: https://npm.io/package/react-select-fetch

## Dependencies (2)

- [qs](https://npm.io/package/qs.md) ^6.13.1
- [krustykrab](https://npm.io/package/krustykrab.md) ^1.1.0

## Alternatives

- [mobx-react](https://npm.io/package/mobx-react.md) — 2.8M weekly downloads
- [rc-tree](https://npm.io/package/rc-tree.md) — 2.6M weekly downloads
- [@react-oauth/google](https://npm.io/package/@react-oauth/google.md) — 1.3M weekly downloads
- [@wagmi/connectors](https://npm.io/package/@wagmi/connectors.md) — 877.0K weekly downloads
- [vee-validate](https://npm.io/package/vee-validate.md) — 836.4K weekly downloads

## Recent versions

- 0.5.6 (latest) — 2024-12-06
- 1.0.0-alpha.3 (alpha) — 2025-10-01
- 0.2.0-alpha.1 (next) — 2020-05-03
- 1.0.0-alpha.2 — 2025-08-11
- 1.0.0-alpha.1 — 2025-06-09
- 1.0.0-alpha.0 — 2025-01-28
- 0.5.5 — 2024-09-18
- 0.5.4 — 2024-07-26
- 0.5.3 — 2024-04-11
- 0.5.2 — 2024-04-11
- 0.5.1 — 2023-06-23
- 0.5.0 — 2022-10-06
- 0.4.0 — 2021-09-28
- 0.3.2 — 2021-07-02
- 0.3.1 — 2020-12-04
- … 6 more at https://npm.io/package/react-select-fetch/versions

## README

[![NPM](https://img.shields.io/npm/v/react-select-fetch.svg)](https://www.npmjs.com/package/react-select-fetch)
[![codecov.io](https://codecov.io/gh/vtaits/react-select-async-paginate/branch/master/graph/badge.svg)](https://codecov.io/gh/vtaits/react-select-async-paginate)
![dependencies status](https://img.shields.io/librariesio/release/npm/react-select-fetch)

# react-select-fetch

Wrapper above `react-select-async-paginate` that loads options from specified url.

## Sandbox examples

- [Simple](https://codesandbox.io/s/9nfmg)
- [Manual control of input value and menu opening](https://codesandbox.io/s/34pjt)
- [Initial options](https://codesandbox.io/s/8cgpp)

## Installation

```
npm install react-select react-select-async-paginate react-select-fetch
```

or

```
yarn add react-select react-select-async-paginate react-select-fetch
```

## Motivation

Abstractions are wonderful but the most common task for async select is load list of options by specified url and query params. E.g.

```javascript
// With SelectFetch

import { SelectFetch } from 'react-select-fetch';

...

<SelectFetch
  value={value}
  url="/awesome-api-url/"
  mapResponse={(response) => ({
    options: response.results,
    hasMore: response.has_more,
  })}
  onChange={setValue}
/>
```

```javascript
// Without SelectFetch

import { AsyncPaginate } from 'react-select-async-paginate';

...

<AsyncPaginate
  value={value}
  loadOptions={async (search, loadedOptions, { page }) => {
    const response = await fetch(`/awesome-api-url/?search=${search}&page=${page}`);
    const responseJSON = await response.json();

    return {
      options: responseJSON.results,
      hasMore: responseJSON.has_more,
      additional: {
        page: page + 1,
      },
    };
  }}
  onChange={setValue}
  additional={{
    page: 1,
  }}
/>
```

## Props

`SelectFetch` receives props of `react-select` and `react-select-async-paginate`. And there are some new props:

### url

Required. String.

### queryParams

Not required. Object. Object of permanent query params for requests.

### searchParamName

Not required. String. Name of param that contains value of search input. `"search"` by default.

### pageParamName

Not required. String. Name of param that contains index of loaded page. Starts from `1`. `"page"` by default.

### offsetParamName

Not required. String. Name of param that contains number of loaded optons. `"offset"` by default.

### mapResponse

Not required. Function. Mapper from server's response to format of `react-select-async-paginate`. Arguments:

  1. `response` - response of server;

  2. `payload` - object:

    - `payload.search` - current search;
    - `payload.prevPage` - page number before requrest;
    - `payload.prevOptions` - options before request;

### initialPage

Not required. Page number for first request for every search. `1` by default.

### defaultInitialPage

Not required. Page number for first request for empty search if `options` or `defaultOptions` defined. `2` by default.

### get

Not required. Async function. Arguments:

1. url;
2. object of query params;

Should return parsed response of server.

Example with `axios`:

```javascript
import axios from 'axios';

...

const get = async (url, params) => {
  const response = await axios.get(url, {
    params,
  });

  return response.data;
};
```

## Replacing react-select component

You can use `withSelectFetch` HOC.

```javascript
import { withSelectFetch } from 'react-select-fetch';

...

const CustomSelectFetch = withSelectFetch(CustomSelect);
```

### typescript

Describing type of component with extra props (example with `Creatable`):

```typescript
import type { ReactElement } from 'react';
import type { GroupBase } from 'react-select';
import Creatable from 'react-select/creatable';
import type { CreatableProps } from 'react-select/creatable';

import type { ComponentProps } from 'react-select-async-paginate';
import { withSelectFetch } from 'react-select-fetch';
import type { UseSelectFetchParams } from 'react-select-fetch';

type SelectFetchCreatableProps<
OptionType,
Group extends GroupBase<OptionType>,
IsMulti extends boolean,
> =
  & CreatableProps<OptionType, IsMulti, Group>
  & UseSelectFetchParams<OptionType, Group>
  & ComponentProps<OptionType, Group, IsMulti>;

type SelectFetchCreatableType = <
OptionType,
Group extends GroupBase<OptionType>,
IsMulti extends boolean = false,
>(props: SelectFetchCreatableProps<OptionType, Group, IsMulti>) => ReactElement;

const SelectFetchCreatable = withSelectFetch(Creatable) as SelectFetchCreatableType;
```

---
_Source: https://npm.io/package/react-select-fetch · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
