All Projects → lostpebble → Pullstate

lostpebble / Pullstate

Licence: mit
Simple state stores using immer and React hooks - re-use parts of your state by pulling it anywhere you like!

Programming Languages

typescript
32286 projects

Projects that are alternatives of or similar to Pullstate

Pure Store
A tiny immutable store with type safety.
Stars: ✭ 133 (-80.53%)
Mutual labels:  hooks, state-management, state
atomic-state
A decentralized state management library for React
Stars: ✭ 54 (-92.09%)
Mutual labels:  hooks, state-management, state
jedisdb
redis like key-value state management solution for React
Stars: ✭ 13 (-98.1%)
Mutual labels:  hooks, state-management, state
Use Global Context
A new way to use “useContext” better
Stars: ✭ 34 (-95.02%)
Mutual labels:  hooks, state-management, state
useSharedState
useSharedState is a simple hook that can be used to share state between multiple React components.
Stars: ✭ 0 (-100%)
Mutual labels:  hooks, state-management, state
react-cool-form
😎 📋 React hooks for forms state and validation, less code more performant.
Stars: ✭ 246 (-63.98%)
Mutual labels:  hooks, state-management, state
eventrix
Open-source, Predictable, Scaling JavaScript library for state managing and centralizing application global state. State manage system for react apps.
Stars: ✭ 35 (-94.88%)
Mutual labels:  hooks, state-management, state
alveron-old
Opinionated Elm-inspired Redux Component Architecture for React
Stars: ✭ 17 (-97.51%)
Mutual labels:  state-management, state
Govern
Component-based state management for JavaScript.
Stars: ✭ 270 (-60.47%)
Mutual labels:  state-management, state
Radioactive State
☢ Make Your React App Truly Reactive!
Stars: ✭ 273 (-60.03%)
Mutual labels:  hooks, state-management
Effector
The state manager ☄️
Stars: ✭ 3,572 (+422.99%)
Mutual labels:  state-management, state
k-ramel
State manager for your components apps, the safe and easy way
Stars: ✭ 20 (-97.07%)
Mutual labels:  state-management, state
Nanny-State
simple state management
Stars: ✭ 68 (-90.04%)
Mutual labels:  state-management, state
React Recomponent
🥫 Reason-style reducer components for React using ES6 classes.
Stars: ✭ 272 (-60.18%)
Mutual labels:  state-management, state
zoov
Use 🐻 Zustand with Module-like api
Stars: ✭ 24 (-96.49%)
Mutual labels:  hooks, state-management
Redux Orm
A small, simple and immutable ORM to manage relational data in your Redux store.
Stars: ✭ 2,922 (+327.82%)
Mutual labels:  state-management, state
Constate
React Context + State
Stars: ✭ 3,519 (+415.23%)
Mutual labels:  hooks, state-management
React Coat
Structured React + Redux with Typescript and support for isomorphic rendering beautifully(SSR)
Stars: ✭ 290 (-57.54%)
Mutual labels:  state-management, server-side-rendering
Easy Peasy
Vegetarian friendly state for React
Stars: ✭ 4,525 (+562.52%)
Mutual labels:  hooks, state-management
Westore
更好的小程序项目架构
Stars: ✭ 3,897 (+470.57%)
Mutual labels:  state-management, state

Pullstate

pullstate

Ridiculously simple state stores with performant retrieval anywhere in your React tree using the wonderful concept of React hooks!

  • ~7KB minified and gzipped! (excluding Immer and React)
  • Built with Typescript, providing a great dev experience if you're using it too
  • Uses immer for state updates - easily and safely mutate your state directly!
  • NEW - Create async actions and use React hooks or <Suspense/> to have complete control over their UI states!

Originally inspired by the now seemingly abandoned library - bey. Although substantially different now- with Server-side rendering and Async Actions built in! Bey was in turn inspired by react-copy-write.

Try out a quick example:

Edit Pullstate Client-only Example

🎉 New documentation site is live!


Let's dive right in

This is taken directly from the documentation site, to give you a quick overview of Pullstate here on github. Be sure to check out the site to learn more.

To start off, install pullstate.

yarn add pullstate

Create a store

Define the first state store, by passing an initial state to new Store():

import { Store } from "pullstate";

export const UIStore = new Store({
  isDarkMode: true,
});

Read our store's state

Then, in React, we can start using the state of that store using a simple hook useState():

import * as React from "react";
import { UIStore } from "./UIStore";

export const App = () => {
  const isDarkMode = UIStore.useState(s => s.isDarkMode);

  return (
    <div
      style={{
        background: isDarkMode ? "black" : "white",
        color: isDarkMode ? "white" : "black",
      }}>
      <h1>Hello Pullstate</h1>
    </div>
  );
};

The argument to useState() over here (s => s.isDarkMode), is a selection function that ensures we select only the state that we actually need for this component. This is a big performance booster, as we only listen for changes (and if changed, re-render the component) on the exact returned values - in this case, simply the value of isDarkMode.


Add interaction (update state)

Great, so we are able to pull our state from UIStore into our App. Now lets add some basic interaction with a <button>:

  return (
    <div
      style={{
        background: isDarkMode ? "black" : "white",
        color: isDarkMode ? "white" : "black",
      }}>
      <h1>Hello Pullstate</h1>
      <button
        onClick={() =>
          UIStore.update(s => {
            s.isDarkMode = !isDarkMode;
          })
        }>
        Toggle Dark Mode
      </button>
    </div>
  );

Notice how we call update() on UIStore, inside which we directly mutate the store's state. This is all thanks to the power of immer, which you can check out here.

Another pattern, which helps to illustrate this further, would be to actually define the action of toggling dark mode to a function on its own:

function toggleMode(s) {
  s.isDarkMode = !s.isDarkMode;
}

// ...in our <button> code
<button onClick={() => UIStore.update(toggleMode)}>Toggle Dark Mode</button>

Basically, to update our app's state all we need to do is create a function (inline arrow function or regular) which takes the current store's state and mutates it to whatever we'd like the next state to be.

Omnipresent state updating

Something interesting to notice at this point is that we are just importing UIStore directly and running update() on it:

import { UIStore } from "./UIStore";

// ...in our <button> code
<button onClick={() => UIStore.update(toggleMode)}>Toggle Dark Mode</button>

And our components are being updated accordingly. We have freed our app's state from the confines of the component! This is one of the main advantages of Pullstate - allowing us to separate our state concerns from being locked in at the component level and manage things easily at a more global level from which our components listen and react (through our useStoreState() hooks).

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