All Projects → jaredpalmer → Mutik

jaredpalmer / Mutik

Licence: mit
A tiny (495B) immutable state management library based on Immer

Programming Languages

typescript
32286 projects

Projects that are alternatives of or similar to Mutik

react-store
A library for better state management in react hooks world
Stars: ✭ 34 (-87.9%)
Mutual labels:  state-management
alveron-old
Opinionated Elm-inspired Redux Component Architecture for React
Stars: ✭ 17 (-93.95%)
Mutual labels:  state-management
Loona
🌕 Application State Management done with GraphQL
Stars: ✭ 270 (-3.91%)
Mutual labels:  state-management
Nanny-State
simple state management
Stars: ✭ 68 (-75.8%)
Mutual labels:  state-management
ember
@microstates/ember - Official Microstates bindings for Ember.js
Stars: ✭ 68 (-75.8%)
Mutual labels:  state-management
use-simple-store
🏬 Simple state management using React Hook
Stars: ✭ 21 (-92.53%)
Mutual labels:  state-management
statebot
Write more robust and understandable programs. Statebot hopes to make Finite State Machines a little more accessible.
Stars: ✭ 24 (-91.46%)
Mutual labels:  state-management
Radioactive State
☢ Make Your React App Truly Reactive!
Stars: ✭ 273 (-2.85%)
Mutual labels:  state-management
litestate
An ambitiously tiny, gizp ~800b, flux-like library to manage your state
Stars: ✭ 31 (-88.97%)
Mutual labels:  state-management
Getx
Open screens/snackbars/dialogs/bottomSheets without context, manage states and inject dependencies easily with Get.
Stars: ✭ 5,578 (+1885.05%)
Mutual labels:  state-management
k-ramel
State manager for your components apps, the safe and easy way
Stars: ✭ 20 (-92.88%)
Mutual labels:  state-management
state-workflow
State Workflow Management Package For Laravel. Inspired by ROAM Horizon State Management Package.
Stars: ✭ 29 (-89.68%)
Mutual labels:  state-management
mini-kotlin
Minimal Flux architecture written in Kotlin.
Stars: ✭ 20 (-92.88%)
Mutual labels:  state-management
react-stateful-component
Functional stateful React components with sideEffect support
Stars: ✭ 19 (-93.24%)
Mutual labels:  state-management
Govern
Component-based state management for JavaScript.
Stars: ✭ 270 (-3.91%)
Mutual labels:  state-management
atomic-state
A decentralized state management library for React
Stars: ✭ 54 (-80.78%)
Mutual labels:  state-management
zedux
⚡ A high-level, declarative, composable form of Redux https://bowheart.github.io/zedux/
Stars: ✭ 43 (-84.7%)
Mutual labels:  state-management
Redux Orm
A small, simple and immutable ORM to manage relational data in your Redux store.
Stars: ✭ 2,922 (+939.86%)
Mutual labels:  state-management
React Recomponent
🥫 Reason-style reducer components for React using ES6 classes.
Stars: ✭ 272 (-3.2%)
Mutual labels:  state-management
react-singleton-hook
Create singleton hook from regular react hook
Stars: ✭ 107 (-61.92%)
Mutual labels:  state-management
Mutik

A tiny (495B) immutable state management library based on Immer

Quick Start

yarn add mutik

or

Edit Mutik

Table of Contents

Example

To use Mutik with React, you'll need to install React and React DOM from the experimental release channel because Mutik uses the recently-merged useMutableSource hook internally.

yarn add [email protected] [email protected]
import React from 'react';
import { render } from 'react-dom';
import { createStore, Provider, useSelector } from 'mutik';

// Create a lil' store with some state
let store = createStore({
  count: 0,
});

// Pass the store to the Provider.
function App() {
  return (
    <Provider store={store}>
      <div>
        <Label />
        <Buttons />
      </div>
    </Provider>
  );
}

// You can mutate the store from anywhere you want to,
// even outside of React code. Mutate is based on immer.
function increment() {
  store.mutate(state => {
    state.count++;
  });
}

// Or you can update it like React.useState's update
function decrement() {
  store.set(prevState => ({
    ...prevState,
    count: prevState.count - 1
  });
}

// You don't need to pass the store down as a prop either
function Buttons() {
  return (
    <React.Fragment>
      <button onClick={decrement}>-</button>
      <button onClick={increment}>+</button>
    </React.Fragment>
  );
}

// Lastly, you can subcribe to "slices" of state with useSelector
// Note: be sure to memoize these with React.useCallback if you need to select based on props
function Label() {
  const selector = React.useCallback(state => state.count, []);
  const count = useSelector(selector);
  return <p>The count is {count}</p>;
}

render(<App />, window.root);

API

createStore<S>(intialState: S): Store<S>

Create a Mutik store given some initial state. The store has the following API you can use in or out of React.

store

Method Description
get() Get the current state. Do not use this inside of React, you should instead use useSelector
set(nextState: S | (prevState: S) => V): void; Set state. This can either take a new value or and updater function (just like React.useState's updater)
on(listener: Function): () => void; Subscribe to store. Pass in a callback function that will be executed on updates. on() returns the unsubscribe function for your convenience.
off(listener: Function): void; Unsubscribe a given listener function
reset(): void Set state back to the initialState used when creating the store
mutate(updater: (draft: Draft) => void | S): void; Immer-style updater function.

useSelector<S, V>(selector: (s: S) => V)

React hook to subscribe to Mutik state. Must be called underneath a Mutik Provider.

const selector = state => state.count;

function Label() {
  const count = useSelector(selector);
  return <p>The count is {count}</p>;
}

You can use props with Mutik selector. For performance, it's a good idea to memoize the selector with React.useCallback. For example:

function User({ id }) {
  const selector = React.useCallback(state => state.users[id], [id]);
  const user = useSelector(selector);
  return <p>The username is {user.name}</p>;
}

<Provider />

Mutik context provider. Pass your store as store prop. For example:

import React from 'react';
import { createStore, Provider } from 'mutik';

// Create a lil' store with some state
let store = createStore({
  count: 0,
});

// Pass the store to the Provider.
function App() {
  return (
    <Provider store={store}>
      <div>{/* ... stuff */}</div>
    </Provider>
  );
}

Author

Inspiration


MIT License

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].