Skip to content

v3.7.7

Choose a tag to compare

@danybeltran danybeltran released this 25 Jul 00:12
· 27 commits to master since this release
3597b63

Fature: Add fetchOptions function

Example usage:

import useFetch, { fetchOptions } from "http-react"

// TS
type TodoType = {
  id: number;
  title: string;
  userId: number;
  completed: boolean;
};

// Create your fetchOptions
const todosFetch = fetchOptions<TodoType[]>({
  default: [], // initial data
  fetcher: () => fetch("https://jsonplaceholder.typicode.com/todos"),
});


// Use that options object
function Todos() {
  const { data, loading, refresh } = useFetch(todosFetch);

  return (
    <div>
      <h2 className="text-2xl font-semibold">Todos</h2>
      <button onClick={refresh}>Refresh todos...</button>
      {data.map((todo) => (
        <div key={"todo" + todo.id}>{todo.title}</div>
      ))}
    </div>
  );
}