use-actions

React hook for action binding into your application.


Keywords
react, hooks, redux, thunk, binding
License
MIT
Install
npm install use-actions@1.1.2

Documentation

useActions ยท GitHub license npm version

Inspired by redux-thunk, action binding for your your application.

Fits well with useReducer for state management. ๐Ÿ˜Ž

Installation

npm install use-actions

Documentation

useActions(actions, dispatch)

actions

Object containing all actions in the Redux Thunk model.

dispatch

The dispatch action. Take a look at useReducer.

Examples

A classical example o usage:

  • Actions
// actions/foo-bar.jsx
const ACTION_TYPE = 'ACTION_TYPE';

export default argument => dispatch => {
    // [...]
    dispatch({ type: ACTION_TYPE, payload: argument });
}
  • Container
import React, { useReducer } from 'useActions';
import useActions from 'use-actions';
import * as actions from '../actions/foobar';
import reducer, { initialState } from '../reducers/foobar';

const FoobarContainer = () => {
    const [state, dispatch] = useReducer(reducer, initialState);
    const { dispatchBindedAction } = useActions(actions, dispatch);
    
    // Runs passing curried dispatch to the action
    return <button onClick={dispatchBindedAction}>Click me</button>;
};

License

React is MIT licensed.