All Projects → venil7 → react-usemiddleware

venil7 / react-usemiddleware

Licence: MIT License
React >=16.7 hook, allowing to use standard Redux middleware with useReducer

Programming Languages

javascript
184084 projects - #8 most used programming language

Projects that are alternatives of or similar to react-usemiddleware

use-mobile-detect-hook
React hook to detect the device type
Stars: ✭ 62 (+226.32%)
Mutual labels:  hooks
react-daterange-picker
A react date range picker to using @material-ui. Live Demo: https://flippingbitss.github.io/react-daterange-picker/
Stars: ✭ 85 (+347.37%)
Mutual labels:  hooks
use-bus
React hook to subscribe and dispatch events accros React components
Stars: ✭ 51 (+168.42%)
Mutual labels:  hooks
modules
Mesos modules examples and open source modules outside of the Apache Mesos source tree.
Stars: ✭ 26 (+36.84%)
Mutual labels:  hooks
hooks4git
Extensible Hook Management System for GIT
Stars: ✭ 27 (+42.11%)
Mutual labels:  hooks
react-europe-2019
Slides and demo app from my keynote
Stars: ✭ 29 (+52.63%)
Mutual labels:  hooks
rn-chat
Chat app made with React Native, NativeBase, Apollo Hooks and Sequelize.
Stars: ✭ 37 (+94.74%)
Mutual labels:  hooks
use-table-tools
React Hooks for building kickass react table components
Stars: ✭ 18 (-5.26%)
Mutual labels:  hooks
reason-hooks-lib
A set of reusable ReasonReact hooks.
Stars: ✭ 31 (+63.16%)
Mutual labels:  hooks
dothub
Stop managing your github config like a mere human
Stars: ✭ 14 (-26.32%)
Mutual labels:  hooks
AppRun
AppDir runtime components
Stars: ✭ 19 (+0%)
Mutual labels:  hooks
max-todos
A basic Todo app developed in React.
Stars: ✭ 19 (+0%)
Mutual labels:  hooks
react-smart-app
Preconfiguration React + Ant Design + State Management
Stars: ✭ 13 (-31.58%)
Mutual labels:  hooks
use-input-mask
use-input-mask.now.sh
Stars: ✭ 17 (-10.53%)
Mutual labels:  hooks
learn-react-typescript
Learning React contents with TypeScript (Hooks, Redux)
Stars: ✭ 15 (-21.05%)
Mutual labels:  hooks
react-awesome-reveal
React components to add reveal animations using the Intersection Observer API and CSS Animations.
Stars: ✭ 564 (+2868.42%)
Mutual labels:  hooks
Perky
Perky a beautiful animated app concept built with lots of love in React Native.
Stars: ✭ 37 (+94.74%)
Mutual labels:  hooks
micro-observables
A simple Observable library that can be used for easy state management in React applications.
Stars: ✭ 78 (+310.53%)
Mutual labels:  hooks
crook
Simple hook management tool made with PHP
Stars: ✭ 60 (+215.79%)
Mutual labels:  hooks
click-outside-hook
A simple react hook to detect click or touch events outside an element.
Stars: ✭ 29 (+52.63%)
Mutual labels:  hooks

React useMiddleware hook

npm version

Redux compatible middleware provider for React >=16.7 Hooks

react-useMiddleware allows you to use all existing Redux middlewares with React's new feature called hooks. It introduces new hook called useMiddleware, which is a wrapper around useReducer, but also allows you to pass an optional list of middlewares to be used.

Install

$ npm install react-usemiddleware --save
$ yarn add react-usemiddleware

API

You can use useMiddleware as a straight replacement for useReducer, and optionally pass 3rd parameter - an array of middlewares to be applied to a dispatch function.

 const [state, dispatch] = useMiddleware(reducer, initialState, middlewares = []);

Takes 3 parameters:

  • reducer, same as passed into useReducer hook
  • initialState, same as passed into useReducer hook
  • middlewares - array of middlewares, eg, [thunk, createLogger, saga]

Example

import React from "react";
import ReactDOM from "react-dom";
import { useReducer, useEffect, useState } from "react";
import { applyMiddleware } from "redux";
import thunk from "redux-thunk";
import { createLogger } from "redux-logger";
import useMiddleware from "react-usemiddleware";

const logger = createLogger();
const middlewares = [thunk, logger];

const initState = {
  data: "not loaded"
};

const reducer = (state, action) => {
  switch (action.type) {
    case "LOAD":
      return { data: "loading..." };
    case "LOADED":
      return { data: action.payload };
    default:
      return state;
  }
};

const loadAction = () => async dispatch => {
  dispatch({ type: "LOAD" });
  const res = await fetch("https://jsonplaceholder.typicode.com/todos/1");
  const { title: payload } = await res.json();
  dispatch(loadedAction(payload));
};

const loadedAction = payload => {
  return { type: "LOADED", payload };
};

function App() {
  const [state, dispatch] = useMiddleware(reducer, initState, middlewares);
  return (
    <div className="App">
      <button onClick={() => dispatch(loadAction())}>LOAD</button>
      <span>{state.data}</span>
    </div>
  );
}

Live example

A live demo can be found here

live demo

Contributions

Please open an Issue or a PR

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].