All Projects → nanxiaobei → Retalk

nanxiaobei / Retalk

Licence: mit
🐤 The Simplest Redux

Programming Languages

javascript
184084 projects - #8 most used programming language

Projects that are alternatives of or similar to Retalk

temperjs
State management for React, made simple.
Stars: ✭ 15 (-91.07%)
Mutual labels:  flux, state
Redux Zero
A lightweight state container based on Redux
Stars: ✭ 1,977 (+1076.79%)
Mutual labels:  flux, state
mafuba
Simple state container for react apps.
Stars: ✭ 20 (-88.1%)
Mutual labels:  flux, state
Flooks
🍸 A state manager for React Hooks
Stars: ✭ 201 (+19.64%)
Mutual labels:  flux, state
Reatom
State manager with a focus of all needs
Stars: ✭ 567 (+237.5%)
Mutual labels:  flux, state
vuex-but-for-react
A state management library for React, heavily inspired by vuex
Stars: ✭ 96 (-42.86%)
Mutual labels:  flux, state
mutable
State containers with dirty checking and more
Stars: ✭ 32 (-80.95%)
Mutual labels:  flux, state
Clean State
🐻 A pure and compact state manager, using React-hooks native implementation, automatically connect the module organization architecture. 🍋
Stars: ✭ 107 (-36.31%)
Mutual labels:  flux, state
Almin
Client-side DDD/CQRS for JavaScript.
Stars: ✭ 477 (+183.93%)
Mutual labels:  flux, state
RxReduxK
Micro-framework for Redux implemented in Kotlin
Stars: ✭ 65 (-61.31%)
Mutual labels:  flux, state
Little State Machine
📠 React custom hook for persist state management
Stars: ✭ 654 (+289.29%)
Mutual labels:  flux, state
Tiny Atom
Pragmatic and concise state management.
Stars: ✭ 109 (-35.12%)
Mutual labels:  flux, state
Stockroom
🗃 Offload your store management to a worker easily.
Stars: ✭ 1,745 (+938.69%)
Mutual labels:  flux
Go Sdk
Dapr SDK for go
Stars: ✭ 149 (-11.31%)
Mutual labels:  state
Contextism
😍 Use React Context better.
Stars: ✭ 141 (-16.07%)
Mutual labels:  state
Android Statefullayout
A custom Android ViewGroup to display different states of screen (CONTENT, PROGRESS, OFFLINE, EMPTY, etc.)
Stars: ✭ 140 (-16.67%)
Mutual labels:  state
Mobx Rest
REST conventions for Mobx
Stars: ✭ 164 (-2.38%)
Mutual labels:  state
React Copy Write
✍️ Immutable state with a mutable API
Stars: ✭ 1,810 (+977.38%)
Mutual labels:  state
Freactal
Clean and robust state management for React and React-like libs.
Stars: ✭ 1,676 (+897.62%)
Mutual labels:  state
Designpatterns
🔑Elements of Reusable Object-Oriented Software🔓is a software engineering book describing software design patterns. The book's authors are Erich Gamma, Richard Helm, Ralph Johnson and John Vlissides with a foreword by Grady Booch.
Stars: ✭ 134 (-20.24%)
Mutual labels:  state
Retalk

The simplest solution for Redux.

Travis Codecov npm version npm bundle size npm downloads license

English | 简体中文


Why

  • Simplest Redux - Same syntax as React components.
  • Only 2 APIs - setStore() and withStore().
  • Async models - Fully code splitting support for models.
  • Auto loading - Auto loading state for async actions.

Install

yarn add retalk

# or

npm install retalk

Usage

1. Model

Usually we'll set several routes in our app, one route with one model, so we'll have several models.

Write the model like a React component, just without the lifecycle methods.

class CounterModel {
  state = {
    count: 0,
  };
  add() {
    // this.state -> Get state of own model
    // this.setState() -> Set state of own model
    // this.someAction() -> Call actions of own model

    // this.models.someModel.state -> Get state of other models
    // this.models.someModel.someAction() -> Call actions of other models

    const { count } = this.state;
    this.setState({ count: count + 1 });
  }
  async addLater() {
    // Auto `someAsyncAction.loading` state can be use

    await new Promise((resolve) => setTimeout(resolve, 1000));
    this.add();
  }
}

2. Store

Use setStore() to set up all models with theirs namespaces.

import { setStore } from 'retalk';

const store = setStore({
  counter: CounterModel,
  // Other models...
});

3. View

Use withStore() to connect models and components.

import React from 'react';
import { withStore } from 'retalk';

const Counter = ({ count, add, addLater }) => (
  <div>
    <p>{count}</p>
    <button onClick={add}>+</button>
    <button onClick={addLater}>+ ⏳{addLater.loading && '...'}</button>
  </div>
);

const CounterWrapper = withStore({
  counter: ['count', 'add', 'addLater'],
})(Counter);

4. App

Use <Provider> to provide the store to your app.

import ReactDOM from 'react-dom';
import { Provider } from 'retalk';

const App = () => (
  <Provider store={store}>
    <CounterWrapper />
  </Provider>
);

ReactDOM.render(<App />, document.getElementById('root'));

Demo

Edit retalk

API

1. setStore()

const store = setStore(models, middleware);

Pass models and middleware(both are optional), Setup the one and only store.

In development mode, Redux DevTools will be enabled by default, make sure its version >= v2.15.3 and not v2.16.0.

const store = setStore(
  {
    home: HomeModel,
    counter: CounterModel,
  },
  [logger, crashReporter]
);

2. withStore()

withStore(...modelNames)(Component)

Eject one or more models' state and actions to a component's props.

There are 3 ways to use it:

Use string to eject all

const CounterWrapper = withStore('home', 'counter')(Counter);

The Simplest way, but if some unused props are injected, it will also trigger a re-render.

This method can be used if determined that all injected props will be used, or to rapid develop.

Use object to customize

const CounterWrapper = withStore({
  home: ['name', 'setName'],
  counter: ['count', 'add', 'addLater'],
})(Counter);

Customize the injected props, only inject the needed props, so to optimize the performance.

Use mapStateToProps()... to customize more

const CounterWrapper = withStore(mapStateToProps, mapDispatchToProps)(Counter);

For more customization of the injected props, you can use mapStateToProps, mapDispatchToProps etc.

At that time, withStore() will be used as connect().

3. Provider & batch()

Just redux-redux's Provider and batch().

You can import them from retalk to simplify development.

FAQ

Async import models?

Setup the store with setStore(), then use libs like loadable-components to import components and models.

Then, use store.add() to eject models to store.

Here is an example with loadable-components:

import React from 'react';
import loadable from 'loadable-components';

const AsyncCounter = loadable(async () => {
  const [{ default: Counter }, { default: CounterModel }] = await Promise.all([
    import('./Counter/index.jsx'),
    import('./Counter/Model.js'),
  ]);
  store.add({ counter: CounterModel }); // Use `store.add(models)`, like `setStore(models)`
  return (props) => <Counter {...props} />;
});

License

MIT © nanxiaobei

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