Giter VIP home page Giter VIP logo

sodayo's Introduction

sodayo

NPM version

A state management for React.

  • Easy-to-use API
  • Based on useSyncExternalStore
  • Lightweight: Gzipped ≈ 0.2KB (CJS + MJS + Types ≈ 0.7KB)

This project is inspired by Pinia and Resso.

Installation

// NPM
$ npm i -S sodayo
// Yarn
$ yarn add sodayo
// PNPM
$ pnpm add sodayo

Docs

Getting Started

Similar to Pinia, sodayo recommends src/stores as a directory for storing the store. You can create this folder first.

Creating a store

Let's create a new store - how about calling it "app"? (You need to create app.(js|ts) under src/stores) Then, we need to initialize it.

// `src/stores/app.ts`
import { atom, defineStore } from "sodayo";

export const useAppStore = defineStore(() => { // See why it's so named below
  const count = atom(0);
  const inc = (n = 1) => { count.value += n; };
  return {
    count,
    inc,
  };
});

If you've used Vue and Pinia, this might be familiar. The way to create this Store comes from Pinia. And atom is a utility function that behaves like ref in @vue/reactivity. In defineStore, you can create functions to make changes to the value properties of these atoms, just as inc does. We call these functions mutations.

Using a store in the component

In a component, you can do like this to access to the store.

// `src/pages/some-page.tsx`
import { useAppStore } from "../stores/app";

function Page() {
  const store = useAppStore();

  return (
    <div>
      {store.count}
      <button type="button" onClick={() => store.inc()}>
        Add 1
      </button>
      <button type="button" onClick={() => store.inc(10)}>
        Add 10
      </button>
    </div>
  );
}

You might be confused - where is the value property of count? In fact, sodayo proxied the values internally so that you don't have to annoy and type .value in your code every time.

PS: By design, you can't modify the count in the component, you can only modify it through the mutation defined in the store. The top-level Proxy does not implement the modification operation on the value.

That's it! You already understand how to use sodayo. Go and develop your project!

Advanced

Getters

Getter atoms are supported since v0.2.0. You can use it like this:

import { atom, defineStore, mota } from "sodayo";

export const useAppStore = defineStore(() => {
  const count = atom(0);
  const doubled = mota(() => count.value * 2);
  const inc = (n = 1) => { count.value += n; };
  return {
    count,
    doubled,
    inc,
  };
});

After that, you can use it like a normal atom.

License

MIT License © 2021 Ray

sodayo's People

Contributors

so1ve avatar

Stargazers

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