All Projects → nathanbuchar → React Hook Thunk Reducer

nathanbuchar / React Hook Thunk Reducer

Licence: mit
📡 A React useReducer() hook whose dispatcher supports thunks à la redux-thunk.

Programming Languages

javascript
184084 projects - #8 most used programming language

Projects that are alternatives of or similar to React Hook Thunk Reducer

Useeffectreducer
useReducer + useEffect = useEffectReducer
Stars: ✭ 642 (+605.49%)
Mutual labels:  hook, reducer
React Selector Hooks
Collection of hook-based memoized selector factories for declarations outside of render.
Stars: ✭ 84 (-7.69%)
Mutual labels:  hook
Reapex
A lightweight framework to build pluggable and extendable redux/react application
Stars: ✭ 58 (-36.26%)
Mutual labels:  reducer
Dynamicoc
深入理解 iOS 热修复原理
Stars: ✭ 76 (-16.48%)
Mutual labels:  hook
Use Reducer X
🔩 An alternative to useReducer that accepts middlewares.
Stars: ✭ 62 (-31.87%)
Mutual labels:  reducer
Jjexception
Protect the objective-c application(保护App不闪退)
Stars: ✭ 1,216 (+1236.26%)
Mutual labels:  hook
Apex Legends Internal
Simple Apex Legends esp source
Stars: ✭ 53 (-41.76%)
Mutual labels:  hook
Commit Msg Linter
git commit message linter hook
Stars: ✭ 87 (-4.4%)
Mutual labels:  hook
Lottie React
A lightweight React library for rendering complex After Effects animations in real time using Lottie.
Stars: ✭ 83 (-8.79%)
Mutual labels:  hook
Use Reducer With Side Effects
Stars: ✭ 75 (-17.58%)
Mutual labels:  reducer
React Hook Mighty Mouse
🐭 React hook that tracks mouse events on selected element - zero dependencies
Stars: ✭ 75 (-17.58%)
Mutual labels:  hook
React Hook Tutorial
React Hook 系列教程,学习和探索Hooks世界。
Stars: ✭ 65 (-28.57%)
Mutual labels:  hook
Lithium
Line-based testcase reducer
Stars: ✭ 80 (-12.09%)
Mutual labels:  reducer
Use Delayed Render
react hook for delaying the render and unmount of a component
Stars: ✭ 62 (-31.87%)
Mutual labels:  hook
Delphihookutils
Delphi Hooking Library by Lsuper
Stars: ✭ 85 (-6.59%)
Mutual labels:  hook
S Mvp
🔥🔥优化版MVP,使用注解泛型简化代码编写,使用模块化协议方便维护,APT过程使用注解解析器利用JavaPoet🌝完成重复模块的编写,利用ASpect+GradlePlugin 完成横向AOP编程+Javassist动态字节码注入+Tinker实现热修复+Retrofit实现优雅网络操作+RxJava轻松玩转数据处理
Stars: ✭ 1,095 (+1103.3%)
Mutual labels:  hook
Wechat Pc Hook Ws
微信PcHooker websocket api
Stars: ✭ 74 (-18.68%)
Mutual labels:  hook
Ysf
YSF Server Functions
Stars: ✭ 77 (-15.38%)
Mutual labels:  hook
Neatinput
A .NET standard project which aims to make keyboard and mouse input monitoring easy on Windows and eventually Linux.
Stars: ✭ 89 (-2.2%)
Mutual labels:  hook
Hookmsrbysvm
hook msr by amd svm
Stars: ✭ 86 (-5.49%)
Mutual labels:  hook

react-hook-thunk-reducer

Akin to redux-thunk, useThunkReducer() augments React's useReducer() hook so that the action dispatcher supports thunks. Now, you can write action creators that return a function rather than an action!

Requires React v16.8 and above.

Npm version Travis David


Install

npm install react-hook-thunk-reducer

Then just import it and use it like you would React.useReducer().

import { useThunkReducer } from 'react-hook-thunk-reducer';

function Component({ initialState }) {
  const [state, dispatch] = useThunkReducer(reducer, initialState);

  // ...
}

Usage

Create your actions just like you would in Redux. Similar to redux-thunk, if an action returns a function, it's treated as a thunk and has access to the current state.

function increment() {
  return {
    type: 'increment'
  };
}

function incrementIfOdd() {
  return (dispatch, getState) => {
    const { count } = getState();

    if (count % 2 !== 0) {
      dispatch(increment());
    }
  };
}

Create your functional component and call the useThunkReducer() hook as if it were React.useReducer();

import { useThunkReducer } from 'react-hook-thunk-reducer';

// ...

function reducer(state, action) {
  switch (action.type) {
    case 'increment':
      return { count: state.count + 1 };
    default:
      throw new Error();
  }
}

function Counter({ initialState }) {
  const [state, dispatch] = useThunkReducer(reducer, initialState);

  // ...

  return (
    <>
      Count: {state.count}
      <button onClick={onButtonPress}>+</button>
    </>
  );
}

Dispatch your actions using the augmented dispatch function.

const onButtonPress = () => {
  dispatch(incrementIfOdd());
};

The value of the inner function will be returned when dispatching thunks.

function incrementAndReturnCount() {
  return (dispatch, getState) => {
    dispatch(increment());

    return getState().count;
  };
}

const newCount = dispatch(incrementAndReturnCount());
Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].