Code Monkey home page Code Monkey logo

flooks's Introduction

๐Ÿธ flooks

A state manager for React Hooks. Maybe the simplest. ^_^

npm Travis (.org) Codecov npm type definitions GitHub

๐Ÿฐ Simple | ๐Ÿญ Auto loading | ๐Ÿ• Modules | ๐Ÿฅ‚ Flexible


English | ็ฎ€ไฝ“ไธญๆ–‡


Install

yarn add flooks

or

npm install flooks

Usage

import { setModel, useModel } from 'flooks';

const counter = {
  state: {
    count: 0,
  },
  actions: ({ model, setState }) => ({
    increment() {
      const { count } = model();
      setState({ count: count + 1 });
    },
    decrement() {
      const { count } = model();
      setState({ count: count - 1 });
    },
    async incrementAsync() {
      const { increment } = model();
      await new Promise((resolve) => setTimeout(resolve, 1000));
      increment();
    },
  }),
};

setModel('counter', counter);

function Counter() {
  const { count, increment, decrement, incrementAsync } = useModel('counter');
  return (
    <>
      Count: {count}
      <button onClick={increment}>+</button>
      <button onClick={decrement}>-</button>
      <button onClick={incrementAsync}>+ async{incrementAsync.loading && '...'}</button>
    </>
  );
}

Demo

Edit flooks

API

setModel()

setModel(name, model);

Accepts a name string and an model object, initialize the model.

The model object needs to contain a state object and an actions function.

useModel()

const { someState, someAction } = useModel(name);

A React Hook. Accepts a name, returns the initialized model with all its states and actions.

({ model, setState }) => realActions

actions: ({ model, setState }) => ({
  someAction() {},
});

The argument of actions contains two functions, model() and setState().

model()

const { someState, someAction } = model(name?);

Returns the same as useModel(), but when get own model, name can be omitted.

i.e. model() for own model, model('other') for other models.

setState()

setState(payload);

Used to update own model's state with the payload object, can't update other models'.

FAQ

Auto loading?

actions: ({ model, setState }) => ({
  async someAsyncAction() {},
});

When an action is async, someAsyncAction.loading can be use.

Code splitting?

Supported naturally. Call setModel() in components, then use libraries like loadable-components.

Set models together?

import { setModel } from 'flooks';
import a from '...';
...

const models = { a, b, c };
Object.entries(models).forEach(([name, model]) => {
  setModel(name, model);
});

This is not recommended. Call setModel() separately in components, which is more clear and flexible.

Philosophy

1. Our philosophy is decentralization, so we recommend to bind a model and a route entry component as one module, call setModel() in the component to bind two.

2. No need to add a file like store.js or models.js, because no need to distribute the store from top now. Without the centralized store, just the modules consisting of components and models in the lower level.

3. A model has its own space, with useModel() and model(), all other models can be reached. Models are independent, but also connected.

4. Don't initialize a model multiple times using setModel(), if have a "common" model used in several places, recommend to to initialize it in an upper component, such as App.jsx.

5. That's all, enjoy it~

License

MIT License (c) nanxiaobei

flooks's People

Contributors

nanxiaobei avatar

Watchers

James Cloos 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.