React Use API
  • Introduction
  • Usage
    • Getting Started
    • Advanced Usage
      • List API and Pagination
      • Request Control
      • Data Processing
      • Side-Effect Dependencies
      • Watch option
    • TypeScript
  • API
    • useApi
    • State
    • Request Function
    • ApiProvider
    • injectSSRHtml
    • loadApiCache
  • Server Side Rendering
    • SSR Settings
    • Examples
      • Real World Examples
      • With Ant Design
Powered by GitBook
On this page

Was this helpful?

  1. Usage
  2. Advanced Usage

List API and Pagination

src/main.jsx
import React, { useState, useMemo, useCallback } from 'react'
import useApi from 'react-use-api'


export const Main = () => {
  const [offset, setOffset] = useState(0)
  const limit = 10
  const options = useMemo(
    () => ({
      handleData,
      dependencies: {
        limit
      }
    }),
    [limit]
  )
  // hasMore is a custom state here
  const [data, { loading, error, hasMore = true }, request] = useApi(
    getAPiList(),
    options
  )
  const loadMore = useCallback(() => {
    const nextOffset = offset + limit
    // fetch the data and keep the state and prevData
    request(getAPiList(nextOffset, limit), true)
    setOffset(nextOffset)
  }, [offset])

  return (
    <>
      {loading && <div>Loading...</div>}
      {error && <div>{error.response.data.errMsg}</div>}
      {data && (
        <>
          {data.list.map(({ name }) => (
            <div key={name}>{name}</div>
          ))}
          {hasMore && <button onClick={loadMore}>Load More</button>}
        </>
      )}
    </>
  )
}

export const getAPiList = (offset, limit) => ({
  url: '/api/list',
  params: {
    offset,
    limit
  }
})

// [IMPORTANT] Using any state setter in handleData is not allowed,
// it will cause the component re-rendering infinitely while SSR rendering.
export const handleData = state => {
  const { prevData = [], response, dependencies, error } = state
  if (!error) {
    const {
      data: { userList }
    } = response
    const { limit } = dependencies
    if (userList.length < limit) {
      state.hasMore = false
    }
    return [...prevData, ...userList]
  } else {
    // show an error message from the api
    console.log(error.response.data.msg)
  }
}
PreviousAdvanced UsageNextRequest Control

Last updated 5 years ago

Was this helpful?