0.0.25 • Published 1 year ago

react-refetch-hooks v0.0.25

Weekly downloads
-
License
-
Repository
-
Last release
1 year ago

react-fetch

GitHub npm Travis (.com)

Quick Start

npm install react-refetch-hooks

Query

import { useQuery } from 'react-refetch-hooks'

function App() {
  const { data, loading, error, update } = useQuery(
    () => fetch('https://jsonplaceholder.typicode.com/todos/1')
  )

  if (loading) return <p>Loading...</p>
  if (error) return <p>Error :(</p>

  return (
    <div>
      <p>title: {data.title}</p>
      <p>completed: {data.completed}</p>
      <button onClick={() => update()}>refresh</button>
    </div>
  )
}

Imperative Query

import { useQuery } from 'react-refetch-hooks'

function App() {
  const { data, loading, error, fetch } = useQuery(
    (page: number) => 'https://jsonplaceholder.typicode.com/todos/1',
    { imperative: true }
  )

  if (loading) return <p>Loading...</p>
  if (error) return <p>Error :(</p>

  return (
    <div>
      <p>title: {data.title}</p>
      <p>completed: {data.completed}</p>
      <button onClick={() => fetch(1)}>refresh</button>
    </div>
  )
}

Mutation

import { useMutation } from 'react-refetch-hooks'

const url = 'https://jsonplaceholder.typicode.com/todos'
const fetcher = (form) => fetch(url, {
  method: 'POST',
  body: form
}).then(res => res.json())

function TodoForm() {
  const { mutating, error, trigger } = useMutation(fetcher)

  if (error) return <p>Error :(</p>

  const handleSubmit = (event) => {
    event.preventDefault()
    const formData = new FormData(event.target)
    try {
      await trigger(formData)
    } catch (err) {
      console.log(err)
    }
  }

  return (
    <form onSubmit={handleSubmit}>
      <label>
        Title: <input name="title" type="text"></input>
      </label>
      <input type="submit" disabled={mutating}>Submit</input>
    </form>
  )
}

Q&A

declarative vs imperative

When you choose whether to use declarative or imperative, the main question you need to answer is what kind of logic it is from the user’s perspective. If this logic is caused by a particular interaction, and the query result just using once and not use for rendering ui, then imperative, otherwise declarative.

inspired by you-might-not-need-an-effect

imperative mode withouts any revalidate options.

Features

Basic

  • Fetch OnMount.
  • Dependent Fetching of data that depends on other.
  • Automatic Refetching.
    • Window focus
    • Network reconnect
    • Interval
  • Request deduplication.
  • Debounced Request
  • Prevents potential race conditions
  • Separate Mutation and Query
  • Triggering the mutation manually

Advanced

  • fallbackData vs placeholderData vs keepPreviousData
  • Global configuration available or per hook call.
  • Built-in cache
  • Infinite: pagination and infinite scroll loading.
  • Error handling, Error Retry
  • Supports optimistic UI functionalities
  • Suspense
0.0.25

1 year ago

0.0.20

2 years ago

0.0.22

1 year ago

0.0.23

1 year ago

0.0.19

2 years ago

0.0.18

2 years ago

0.0.17

2 years ago

0.0.16

2 years ago

0.0.15

2 years ago

0.0.14

2 years ago

0.0.13

2 years ago

0.0.12

2 years ago

0.0.11

2 years ago

0.0.10

2 years ago

0.0.9

2 years ago

0.0.8

2 years ago

0.0.7

2 years ago

0.0.6

2 years ago

0.0.5

2 years ago

0.0.4

2 years ago

0.0.3

2 years ago

0.0.2

2 years ago

0.0.1

2 years ago