Code Monkey home page Code Monkey logo

dawei's Introduction

dawei... because it is da wei

npm

Warning! for concurrent react must use version >= 0.14.0

versions less 0.14.0 can fail due to the way forceUpdate was written

the api has stabilized and is pretty solid at this point and I don't forsee any more breaking changes needing to be made. If that remains the case for a while that's cause for version 1.

Simple and fast state management for React.

Inspired by Zustand and Recoil. without the context or the fluff.

import { createStore } from 'dawei';

const formStore = createStore(/* this can be a value, a function or, nothing */);

// Use them anywhere in your app and they stay synced
const Input = () => {
  const [name, setName] = formStore.use('name');
  const [email, setEmail] = formStore.use('email');

  //  you can get and set deeply nested values too, even if they don't exist
  const [companyName, setCompanyName] = formStore.use('company.name');

  return (
    <form>
      <input value={name} onChange={e => setName(e.target.value)} />
      <input value={email} onChange={e => setEmail(e.target.value)} />
      <input value={companyName} onChange={e => setCompanyName(e.target.value)} />
    </form>
  );
};

function randomBitOfApi() {
  // you can update a bit of state from anywhere
  formStore.set({ saved: true });
}

// you can also subscribe to all changes
let unsubscribe = formStore.subscribe(state => {
  console.log('formStore changed', state);
});

API

store = createStore(initialState, storeName?: string)

  • initialState: Function | Object
    • initial state object or a function that returns initial state
    • (set: DaweiSetter, get: DaweiGetter) => initialState

store

  • get(selector string | function)
    • get value from the store, can be a pathed string into a nested store, or an accessor function
    • example: (both are equal)
      • store.get((store) => store.value.wanted)
      • store.get('value.wanted')
  • set(value, selector?)
    • set store value, can be any value, it will get merged into the current state
    • value - any value to set
    • selector - primarily used internally for use, can be a selector string similar to get to scope where the set value is to be applied
  • [value, setValue] = use(selector string | function)
    • use is a react hook that can be thought of similarly to react.useState(), except it uses a selector instead of a default value
    • selector - the store selector can be a pathed string or an accessor functon
      • example: ( both are equal) string selectors are preferred
        • store.use(state => state.value.wanted)
        • store.use('value.wanted')
    • returns
      • value - the value referenced by the selector
      • setValue

        WARNING! slightly different based on type of selector used

        • selector string - returns a scoped setter that will set value at the specified path
        • selector function - returns store.set
  • subscribe(listener: Function, receiveInitial: boolean)
    • subscribe to all store changes
    • listener(value: store values) - called for every change
    • receiveInitial - whether listener should be called with current state on subscribe
  • resolve()
    • mostly used internally or for testing libraries
    • since all set values accept async functions the entire store is promise based and is eventually consistent. chaining on resolve will guarentee that all of the changes made to the store will have settled.

Changelog

Version 0.17.0 fix: when the root was an array changes were incorrect, arrays now use assignment at root instead of Object.assign()

Version 0.16.0 feature: basic typings on use

Version 0.15.0 feature: basic inferred types on get based on intial store creation chore: switched to using pnpm for development

Version 0.11.0

chainMerge was rewritten to better handle pathing into arrays and now treats all numeric values in paths as array indexes

fixes a bug in version 0.10.0-alpha where promises had to be wrapped in functions

// {"arr":[]}
store.set({ test: 'through array' }, 'arr.0.test.1.ok');
//old chainMerge
// {"arr":[{"test":{"1":{"ok":{"test":"through array"}}}}]}

// new chainMerge
// {"arr":[{"test":[null,{"ok":{"test":"through array"}}]}]}

Version 0.10.0-alpha

This release makes use of unstable_batchedUpdates from react-dom to limit the number of rerenders that happen from making changes. This release also works slightly differently, it will merge synchronously if the update is a function

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.