Code Monkey home page Code Monkey logo

minact's Introduction

Minact

Build Size Version Downloads

A simple react state management library without a provider

Demo

https://codesandbox.io/s/minact-demo-b3f6nd

Installation

npm install minact
# or
# yarn add minact

Usage

Create a store

import { createStore } from "minact";

export const { useSelector, useDispatch } = createStore(
  { count: 0 },
  (set, get) => ({
    increase: (amount) => set({ count: get().count + (amount || 1) }),
  })
);

Use the hooks inside your components

const View = () => {
  const count = useSelector((state) => state.count);

  return <div>Count: {count}</div>;
};

const Controls = () => {
  const increase = useDispatch((reducers) => reducers.increase);

  return <button onClick={() => increase()}>Increase</button>;
};

Async actions

Just call the set function to update the store, async functions don't matter.

createStore({ data: null }, (set, get) => ({
  update: async (url) => {
    const res = await fetch(url);
    const data = await res.json();
    set({ data });
  },
}));

Multiple selectors

const App = () => {
  const { count, user } = useSelector((state) => ({
    count: state.count,
    user: state.user,
  }));

  // or

  const [ count, user ] = useSelector((state) => [
    count: state.count,
    user: state.user,
  ]);

  // useDispatch also works

  const { increase, decrease } = useDispatch((reducers) => ({
    increase: reducers.increase,
    decrease: reducers.decrease,
  }));

  // or

  const [ increase, decrease ] = useDispatch((reducers) => [
    increase: reducers.increase,
    decrease: reducers.decrease,
  ]);
};

Usage outside of react component

const store = createStore({ count: 0 }, (set, get) => ({
  increase: () => set({ count: get().count + 1 }),
}));

// Subscribe to store changes and log the state
const unsubscribe = store.subscribe(() => console.log(store.get()));

// Unsubscribe from changes
unsubscribe();

Scales very well

src
│── store
│   │── user-store.js
│   │── count-store.js
│   │── any-store.js

You can create as many store as you want, they will work independently from each other

minact's People

Contributors

napthedev avatar

Stargazers

 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.