0.1.1 • Published 5 years ago

react-use-thunk-reducer v0.1.1

Weekly downloads
10
License
-
Repository
github
Last release
5 years ago

useThunkReducer

To install run

npm i react-use-thunk-reducer

or

yarn add react-use-thunk-reducer

Usage

import React from "react";
import useThunkReducer from "react-use-thunk-reducer";

const reducer = (state, action) => {
  if (action.type === "HI") {
    return action.value;
  } else {
    return state;
  }
};

const changeValueAsync = () => ({ dispatch, getState }) => {
  setTimeout(() => {
    dispatch({ type: "HI", value: Math.random() });
  }, 1000);
};

const App = () => {
  const [state, thunk] = useThunkReducer(reducer, "INITIAL");
  return <button onClick={() => thunk(changeValueAsync())}>{state}</button>;
};

export default App;