1.0.4 • Published 5 years ago

hoodux v1.0.4

Weekly downloads
2
License
MIT
Repository
github
Last release
5 years ago

Hoodux

Hoodux is a helper to replace the React Redux with React hooks.

Usage

Install

$ npm install hoodux --save

or

$ yarn add hoodux

API

  1. useHooduxProvider(reducer, initState)

This is a hook to generate Hoodux Provider which should be placed on the top level of app. Hoodux provider will populate a state and dispatch to the app's components tree.

  1. useHoodux()

This is a hook to pop a state and dispatch to use in any component in your app.

Example

import { useHooduxProvider, useHoodux } from "hoodux";

const initState = {
  isSignedIn: false,
  count: 0
};

const reducer = (state, action) => {
  switch (action.type) {
    case "auth":
      return { isSignedIn: !state.isSignedIn };
    case "changeCount":
      return { count: action.payload };
    default:
      throw new Error("Invalid action type");
  }
};

const App = () => {
  const { HooduxProvider } = useHooduxProvider(reducer, initState);
  return (
    <HooduxProvider>
      <Main />
    </HooduxProvider>
  );
};

const Main = () => {
  const { state, dispatch } = useHoodux();

  return (
    <div>
      <button onClick={() => dispatch({ type: "auth" })}>Toggle</button>
      <button onClick={() => dispatch({ type: "changeCount", payload: 5 })}>
        Change count to 5
      </button>
    </div>
  );
};

Next to do

  • Add more types
1.0.4

5 years ago

1.0.3

5 years ago

1.0.2

5 years ago

1.0.1

5 years ago

1.0.0

5 years ago