3

I am using react-select-async-paginate:

async function loadOptions(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,
    },
  };
}

<AsyncPaginate
  value={value}
  loadOptions={loadOptions}
  onChange={setValue}
  additional={{
    page: 1,
  }}
/>

How to apply the typescript for this code: (search, loadedOptions, { page }) taking into account that these are the types?

demo: https://codesandbox.io/s/react-select-async-paginate-forked-qiizl?file=/src/SelectAsyncPaginate.tsx

1 Answer 1

1

As you can see loadOptions type is LoadOptions<OptionType, Additional>.

That's how you should type your loadOptions function

const loadOptions: LoadOptions<any, {page: number}> = ...

You should replace any with the actual option's type (you can figure it from the API response)

https://codesandbox.io/s/react-select-async-paginate-forked-00k7m?file=/src/SelectAsyncPaginate.tsx

Sign up to request clarification or add additional context in comments.

2 Comments

Looks like you got some comments there. Let me know if you need more help

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.