Giter Site home page Giter Site logo

forimmer's Introduction

forimmer build status npm types

Immutable state store for react apps with suspense integration.

What is this?

forimmer is a experimental functional state store for react apps, which uses immer for immutability and integrates with the new react suspense API. It was in part inspired by the excellent pullstate.

The name is just a silly play with words. "immer" means "always" or "ever" in German and "für immer" means "forever". So, forimmer is basically just a fun, bad translation (that you might encounter from actual Germans trying to speak English). I'm not good at naming things.

How does it work?

First, here is a codesandbox with a very contrived example: link

You can install forimmer from npm:

npm install forimmer -s

Creating a store

A new store is created with the createStore function:

import { createStore } from "forimmer";

const store = createStore<{
  foo: string;
  bar: string;
}>();

You can also supply an initial state:

import { createStore } from "forimmer";

const store = createStore<{
  foo: string;
  bar: string;
}>({foo: "foo"});

If your initial state covers the entire desired State interface, you may omit the generics and createStore will infer the type of your state:

import { createStore } from "forimmer";

const store = createStore({foo: "foo", bar: "bar"});

Using the store state in your react app:

If you just want to access the current state of the store, use:

const state = store.getCurrentState();

The useStoreState hooks allows you to pick values from the store (returning them in an array) and subscribe to changes thereof:

function SomeComponent() {
  const [foo] = useStoreState(store, state => [state.foo]); // useStoreState will infer the type of foo
  return <div>{foo}</div>
}

Whenever state.foo changes (and not any other value), the component will re-render and foo will be updated to the new value.

Suspense integration

If any of the values returned from useStoreState is undefined OR if trying to access a value deeper in the state tree would throw an error, useStoreState will throw a Promise which resolves once the store has been updated and the desired values become available.

You can wrap your component in React.Suspense to show a fallback UI that will automatically appear when useStoreState throws and the loading Promise is pending:

function ComponentWithSuspense() {
  return <React.Suspense fallback={<div>...loading foo</div>}>
    <SomeComponent />
  </React.Suspense>
}

Modifying the store state with actions

In order to modify the store state, you can define store actions. A store action is an async function (that might for instance fetch data from an API) which returns a state recipe, which in turn is a function which is passed a draft state to modify (see the immer docs):

// this will create a store action with a payload:
const setFoo = store.createStoreAction(
  async (newFoo: string) => draft => {
    draft.foo = newFoo;
  }
);

setFoo("foo") // set store.foo to "foo"

const fetchFoo = store.createStoreAction(
  async () => {
    const foo:string = await someApiCall();
    return draft => {
      draft.foo = newFoo;
    }
  }
);
fetchFoo() // fetch foo from some api

forimmer's People

Contributors

dependabot[bot] avatar j-spang avatar michael-klein avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

Forkers

j-spang

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.