Giter VIP home page Giter VIP logo

hinten's Introduction

๐Ÿ” Hinten is a small, scoped react state managment system using hooks and a simple api.

Index

Features

  • < 2kB gzip, gets checked thanks to bundlesize
  • Scoping of state and actions
  • Simple small API
  • Usable for big states
  • Only select what you need
  • Full typescript support

Installation

npm install hinten

or

yarn add hinten

Usage

Store creation

// useStore.js
import createStore, { action } from 'hinten';

const useStore = createStore({
  // State
  count: 1,

  // Actions which change the state
  increment: action((state) => ({ count: state.count + 1 })),
  // Actions can have a payload, which will be placed in the second argument
  setCount: action((state, count) => ({ count })),

  // Scope
  sideMenu: {
    isOpen: false,
    maxItems: 100,

    // This action has the same key as the action above
    // If the action above gets called, this gets too
    // If the action in this scope gets called, the above won't get called
    increment: action((state) => ({ maxItems: state.maxItems + 1 })),
    toggle: action((state) => ({ isOpen: !state.isOpen })),

    child: {
      isExpanded: false,

      toggle: action((state) => ({ isExpanded: !state.isExpanded })),
    },
  },
});

export default useStore;

Store usage

// A.jsx
import React from 'react';
import useStore from './useStore';

export default ({ ...props }) => {
  const { state, dispatch } = useStore();

  return (
    <div {...props}>
      <button
        type="button"
        onClick={() => {
          // The payload is the first argument
          dispatch.setCount(9);

          // increment in sideMenu will get called
          // it won't get called in the scope above
          dispatch.sideMenu.icrement();

          // toggle in sideMenu and in child will get called
          dispatch.sideMenu.toggle();
        }}
      >
        Test me!
      </button>

      {state.count}
      {state.sideMenu.isOpen}
    </div>
  );
}

Scoped store usage

// B.jsx
import React from 'react';
import useStore from './useStore';

export default ({ ...props }) => {
  // If you don't need the entire store you can simple select a part of it
  // This component will only get rerender if something changes in it's selected part
  const { state, dispatch } = useStore('sideMenu.child');

  return (
    <div {...props}>
      <button
        type="button"
        onClick={() => dispatch.toggle()}
      >
        Test me!
      </button>

      {state.isExpanded}
    </div>
  );
}

hinten's People

Contributors

manpenaloza avatar

Stargazers

 avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.