All Projects → rootstrap → redux-tools

rootstrap / redux-tools

Licence: MIT License
Redux tools to speed up development.

Programming Languages

javascript
184084 projects - #8 most used programming language

Projects that are alternatives of or similar to redux-tools

Hooks
Async middleware for JavaScript and TypeScript
Stars: ✭ 117 (+631.25%)
Mutual labels:  hook, middleware
restana
Super fast and minimalist framework for building REST micro-services.
Stars: ✭ 380 (+2275%)
Mutual labels:  middleware
request-context
Simple connect middleware for accessing data in a request context.
Stars: ✭ 55 (+243.75%)
Mutual labels:  middleware
node-uploadx
Node.js middleware for handling resumable uploads
Stars: ✭ 17 (+6.25%)
Mutual labels:  middleware
use
Easily add plugin support to your node.js application.
Stars: ✭ 25 (+56.25%)
Mutual labels:  middleware
cute
An event-centric publisher/subscribe model for objects inspired by the Qt framework
Stars: ✭ 37 (+131.25%)
Mutual labels:  middleware
creviceapp
Multi purpose utility which supports gestures with mouse and keyboard.
Stars: ✭ 22 (+37.5%)
Mutual labels:  hook
react-redux-api-tools
A set of tools to facilitate react-redux development and decouple logic from compontents
Stars: ✭ 37 (+131.25%)
Mutual labels:  middleware
http-authentication
PSR-15 middleware to implement Basic and Digest Http authentication
Stars: ✭ 29 (+81.25%)
Mutual labels:  middleware
formalizer
React hooks based form validation made for humans.
Stars: ✭ 12 (-25%)
Mutual labels:  hook
koa-rest-router
Most powerful, flexible and composable router for building enterprise RESTful APIs easily!
Stars: ✭ 67 (+318.75%)
Mutual labels:  middleware
oak-middleware-jwt
Oak middleware for JWT
Stars: ✭ 24 (+50%)
Mutual labels:  middleware
access-log
PSR-15 middleware to generate access logs
Stars: ✭ 21 (+31.25%)
Mutual labels:  middleware
dictator
Dictates what your users see. Plug-based authorization.
Stars: ✭ 77 (+381.25%)
Mutual labels:  middleware
rusty-hook
git hook manager, geared toward Rust projects
Stars: ✭ 93 (+481.25%)
Mutual labels:  hook
conventional-commits
A PHP library for creating and validating commit messages according to the Conventional Commits specification. Includes a CaptainHook plugin!
Stars: ✭ 128 (+700%)
Mutual labels:  hook
falcon-policy
Policy Middleware for Falcon APIs
Stars: ✭ 30 (+87.5%)
Mutual labels:  middleware
ASPNETcoreAngularJWT
Angular in ASP.NET Core with JWT solution by systemjs
Stars: ✭ 48 (+200%)
Mutual labels:  middleware
DevOps
DevOps code to deploy eScience services
Stars: ✭ 19 (+18.75%)
Mutual labels:  middleware
think-trace
Error trace for ThinkJS 3.x
Stars: ✭ 12 (-25%)
Mutual labels:  middleware

@rootstrap/redux-tools

This package has some basic functionality common to both react bases. It includes a status reducer that lets you track the status of your async actions and a thunks-like middleware that will automatically dispatch success and failure actions.

Basic usage

ActionCreators

This package provides an action creator utility, that together with the provided middleware will make it very easy to create side effects for your actions. This setup will automatically execute your side effect thunk and dispatch success or error actions when the thunk succeeds or fails, respectively.

createThunk receives the action names prefix as the first argument and the async thunk as the second one.

Example:

// src/actions/userActions.js
import { createThunk } from '@rootstrap/redux-tools'

export const getProfile = createThunk(
  'GET_PROFILE',
  userService.getProfile,
);

You can then dispatch this getProfile action, and the middleware will automatically dispatch actions with types GET_PROFILE_SUCCESS or GET_PROFILE_ERROR for you.

The returned object, (getProfile in the example above) has 4 properties you can use in order to handle the different dispatched actions in your reducer:

  • request
  • success
  • error
  • reset

Following the previous example:

// src/reducers/userReducer.js

import { getProfile } from 'src/actions/userActions';

const actionHandlers = {
  [getProfile.success]: (state, { payload }) => {
    state.user = payload;
  },
};

If you need to access the store, or dispatch extra actions from your thunk, you can use dispatch and getState as the last two parameters.

Example:

Dispatching some custom analytics event that requires store data:

// src/actions/userActions.js
import { createThunk } from '@rootstrap/redux-tools'

export const getProfile = createThunk(
  'GET_PROFILE',
  async (userId, dispatch, getState) => {
    const { analytics: { analyticsToken } } = getState();
    const profile = await userService.getProfile(profileId);
    dispatch(analytics.logProfile(analyticsToken, profile));
    return profile;
  },
);

Status tracking

To access status information on a component the useStatus hook is provided. The following status constants are exported:

  • LOADING
  • SUCCESS
  • ERROR

Here is a simple example:

import { useStatus, useDispatch } from 'hooks';
import { getProfile } from 'src/actions/userActions';
import { SUCCESS, LOADING, ERROR } from '@rootstrap/redux-tools'

const MyComponent = () => {
  const getProfileRequest = useDispatch(getProfile);
  const { status, error } = useStatus(getProfile);

  return <>
    <button onClick={getProfileRequest}>Show profile!</button>
    {(status === LOADING) && <Loading />}
    {(status === SUCCESS) && <ProfileComponent />}
    {(status === ERROR) && <ErrorComponent />}
  </>
}

A useLoading hook is also available if you only care about loading status. It returns a boolean indicating whether the action is still loading or not.

To reset the status of an action you can dispatch the reset action returned by createThunk.

Installation guide

Step 1: install the package

npm i @rootstrap/redux-tools or yarn add @rootstrap/redux-tools

Step 2: configure the reducer

// src/reducers/index.js
import { combineReducers } from 'redux'
import { statusReducer } from '@rootstrap/redux-tools'

const rootReducer = combineReducers({
  // ...your other reducers here
  // you have to pass statusReducer under 'statusReducer' key,
  statusReducer
})

Step 3: configure the middleware

import { createStore, applyMiddleware } from 'redux'
import { thunkMiddleware } from '@rootstrap/redux-tools'

import rootReducer from 'src/reducers/index'

const store = createStore(rootReducer, applyMiddleware(thunkMiddleware))

License

@rootstrap/redux-tools is available under the MIT license. See the LICENSE file for more info.

Credits

@rootstrap/redux-tools is maintained by Rootstrap with the help of our contributors.

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