Skip to content

🐶 Simple and handful React hook, for data fetching state tracking

Notifications You must be signed in to change notification settings

moralesbang/use-request

Repository files navigation

use-request

Custom hooks for fetch data

NPM JavaScript Style Guide

Usage

function MyComponent() {
  const [todos, fetchTodos, setTodos] = useRequest({
    service: MyService.getTodos
  })

  if (todos.fetching) {
    return <h1>Loading...</h1>
  }

  if (todos.error) {
    return <h1>Oops! Something was wrong</h1>
  }

  const handleDeleteTodo = (todoId) => {
    const newTodos = todos.data.filter((todo) => todo.id !== todoId)
    setTodos({ data: newTodos })
  }

  return (
    <div>
      <TodoList todos={todos.data} onDelete={handleDeleteTodo} />
      <Button onClick={fetchTodos}>Refresh</Button>
    </div>
  )
}

License

MIT © moralesbang


This hook is created using create-react-hook.

About

🐶 Simple and handful React hook, for data fetching state tracking

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published