Skip to content

Instantly share code, notes, and snippets.

@armouti
armouti / useThroughRedux.ts
Last active April 25, 2022 11:48
A hook to help in migrating from Redux to SWR, use it before the return value inside the custom hooks that wrap useSWR to loop the value through redux
import { useEffect, DependencyList } from "react";
import { useDispatch, useSelector } from "react-redux";
import { Action } from 'redux';
import { IGlobalState } from "../../../redux/GlobalState";
type Selector<T> = (state: IGlobalState) => T;
/**
* @param action The redux action object, pass null or undefined to prevent dispatch
* @param selector A selector function, it should return the state targeted by the 'action' argument