ReSift

ReSift

  • Docs
  • API
  • Help
  • GitHub

›Main Concepts

Introduction

  • What is ReSift?
  • Installation

Tutorial

  • ReSift Rentals

Main Concepts

  • What's a fetch?
  • How to define a fetch
  • Making state consistent
  • Making sense of statuses
  • What are data services?
  • Error handling
  • Custom hooks

Examples

  • ReSift Notes (CRUD)
  • Infinite scroll
  • Custom hooks and React Router

Guides

  • ReSift vs Apollo and Relay
  • HTTP proxies
  • Usage with TypeScript
  • Usage with Redux
  • Usage with classes

API

  • About these docs
  • useStatus
  • useError
  • useDispatch
  • useData
  • useClearFetch
  • isUnknown
  • isNormal
  • isLoading
  • isError
  • defineFetch
  • dataServiceReducer
  • createStoreKey
  • createHttpService
  • createHttpProxy
  • createDataService
  • createActionType
  • combineStatuses
  • ResiftProvider
  • Guard
  • CanceledError
  • UNKNOWN
  • NORMAL
  • LOADING
  • ERROR
Edit

Custom hooks

The last concept we want to highlight is custom hooks.

React hooks are great because they allow you to build on top of other existing hooks. They allow you to generalize many of the common tasks you'll do in your application.

This is why ReSift's APIs are hooks based — so you can build on top of them and combine them with other custom hooks.


Below is a simple demo of a list. When you select an item from the list, the details show up on the right.

The data in this list is powered by the custom hook useCurrentPerson.

useCurrentPerson combines ReSift's useData hook with React Router's useRouteMatch hook to grab the current item using an ID from URL.

useCurrentPerson.js

import { useEffect } from 'react';
import { useData, useDispatch, useStatus } from 'resift';
import { useRouteMatch } from 'react-router-dom';
import makeGetPerson from './makeGetPerson';

// this 👇 is the custom hook 🎉
function useCurrentPerson() {
  const dispatch = useDispatch();

  // it uses `useRouteMatch` from `react-router`...
  const match = useRouteMatch('/people/:id');
  const id = match ? match.params.id : null;
  const getPerson = id ? makeGetPerson(id) : null;

  // ...along with `useData` from resift to join values!
  const data = useData(getPerson);
  const status = useStatus(getPerson);

  // it does dispatching as well
  useEffect(() => {
    if (getPerson) {
      dispatch(getPerson());
    }
  }, [dispatch, getPerson]);

  return { data, status };
}

export default useCurrentPerson;

Demo


⚠️ Custom hooks are great… but, as with any abstraction, it's easy to get carried away.

Be deliberate, when you create any abstractions aka avoid hasty abstractions!

Last updated on 11/4/2019
← Error handlingReSift Notes (CRUD) →
  • Demo
ReSift
Docs
What is ReSift?What's a fetch?API
Community
Ask a questionOpen an issue@ meLeave feedback
More
StarBuild StatusCoverage Status
Copyright © 2021 Sift
www.justsift.com