Code Monkey home page Code Monkey logo

azux's Introduction

Azux

A small state management that is inspired on unstated-next

Install

npm install --save azux

Example

import React, { useState } from "react";
import { useStore, Provider } from "azux";
import { render } from "react-dom";

function CounterStore(initial = 0) {
  const [count, setCount] = useState(initial);
  const decrement = () => setCount(count - 1);
  const increment = () => setCount(count + 1);
  return { count, decrement, increment };
}

function CounterDisplay() {
  const { count, increment, decrement } = useStore(CounterStore);
  return (
    <div>
      <button onClick={decrement}>-</button>
      <span>{count}</span>
      <button onClick={increment}>+</button>
    </div>
  );
}

function App() {
  return (
    <Provider store={CounterStore}>
      <CounterDisplay />
      <Provider store={CounterStore} initial={2}>
        <div>
          <div>
            <CounterDisplay />
          </div>
        </div>
      </Provider>
    </Provider>
  );
}

render(<App />, document.getElementById("root"));

API

Provider

Passing store and its initial state

<Provider store={CountStore} initial={0} />

Passing multiple stores and their initial states

<Provider
  store={{ CountStore, GreetingStore }}
  initial={{ CountStore: 0, GreetingStore: "World" }}
/>

Passing store tuples

<Provider
  store={[
    [CountStore, 0],
    [GreetingStore, "World"],
  ]}
/>

useStore()

Retrieve store api

function Component() {
  // this component will rerender whenever store api updated
  const { count, increment, decrement } = useStore(CounterStore);
}

Use selector to retrieve specific store prop

function Component() {
  // this component will rerender whenever count prop changed
  const count = useStore(CounterStore, (api) => api.count);
}

Advanced Usages

Composing stores

const CountStore = (initial) => {
  const [count, setCount] = useState(initial);
  return {
    count,
    increase() {
      setCount(count + 1);
    },
  };
};

const GreetingStore = (initial) => {
  const [name, setName] = useState(initial);
  return {
    name,
    updateName: setName,
  };
};

const MainStore = ({ name, count } = {}) => {
  // extend stores with initial values
  const counter = useStore(CountStore, count);
  const greeting = useStore(GreetingStore, name);
  return {
    ...counter,
    ...greeting,
  };
};

azux's People

Watchers

 avatar  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.