1.0.12 • Published 4 years ago

react-hookz v1.0.12

Weekly downloads
7
License
MIT
Repository
github
Last release
4 years ago

React-Hookz

Build Status Language grade: JavaScript npm version install size bundle size downloads npm.io

react-hookz

React Global Hookz, a simple global state for React with the Hooks API in less than 1kb written in TypeScript


Table of Contents

Installation

npm install react-hookz

Usage

Minimal example:

Actions

export const addToCounter = (store: any, amount: number) => {
  const counter = store.state.counter + amount;
  store.setState({ counter });
};

HOC

import React from "react";
import ReactHookz from "react-hookz";

import * as actions from "../actions/index";

export interface GlobalState {
  counter: number;
}
const initialState: GlobalState = {
  counter: 1
};

const useReactHookz = ReactHookz(React, initialState, actions);
export const connect = Component => {
  return props => {
    let [state, actions] = useReactHookz();
    let _props = { ...props, state, actions };
    return <Component {..._props} />;
  };
};

export default useReactHookz;

Component

import React from "react";
import { connect } from "../store";

interface Props {
  state: any;
  actions: any;
}

const Counter: React.FC<Props> = props => {
  const { state, actions } = props;
  return (
    <div className="Counter">
      <p>
        FC Counter:
        {state.counter}
      </p>
      <button type="button" onClick={() => actions.addToCounter(1)}>
        +1 to global
      </button>
    </div>
  );
};

export default connect(Counter);

Examples

1.0.12

4 years ago

1.0.11

4 years ago

1.0.10

4 years ago

1.0.9

4 years ago

1.0.8

4 years ago

1.0.7

4 years ago

1.0.6

4 years ago

1.0.5

4 years ago

1.0.4

4 years ago

1.0.3

4 years ago

1.0.2

4 years ago

1.0.1

4 years ago

1.0.0

4 years ago