retalk
The Simplest Redux
Last updated 18 days ago by nanxiaobei .
MIT · Repository · Bugs · Original npm · Tarball · package.json
$ cnpm install retalk 
SYNC missed versions from official npm registry.
Retalk

Simplest solution for Redux, write Redux like React.

Travis Codecov npm version npm bundle size npm downloads license

English | 简体中文


Why

  • Simplest Redux - Same syntax as a React component.
  • Only 2 API - 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. Models

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 setup all models with theirs namespaces.

import { setStore } from 'retalk';

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

3. Views

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:

2.1. Use string to eject all

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

Simplest way, but if some unused props are injected, it will also trigger a re-rendering to affect performance.

This method can be used if it is determined that all injected props will be used, or rapid development will be given priority rather than performance.

2.2. 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 as to optimize the performance.

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

1. Async import models?

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

Then, use store.add(models) to eject the imported models to the 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} />;
});

2. Support HMR?

Change the entry file index.js to:

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

render();

if (module.hot) {
  module.hot.accept('./App', () => {
    render();
  });
}

Make sure that <Provider> is inside the <App> component:

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

License

MIT © nanxiaobei

Current Tags

  • 3.2.2                                ...           latest (18 days ago)

26 Versions

  • 3.2.2                                ...           18 days ago
  • 3.2.1 [deprecated]           ...           18 days ago
  • 3.2.0                                ...           6 months ago
  • 3.1.0                                ...           7 months ago
  • 3.0.0                                ...           7 months ago
  • 2.3.1                                ...           9 months ago
  • 2.3.0                                ...           10 months ago
  • 2.2.4                                ...           a year ago
  • 2.2.2                                ...           a year ago
  • 2.2.1                                ...           a year ago
  • 2.2.0                                ...           a year ago
  • 2.1.0                                ...           a year ago
  • 2.0.0                                ...           a year ago
  • 1.2.3                                ...           a year ago
  • 1.2.2                                ...           a year ago
  • 1.2.1                                ...           a year ago
  • 1.2.0                                ...           a year ago
  • 1.1.0                                ...           2 years ago
  • 1.0.8                                ...           2 years ago
  • 1.0.7                                ...           2 years ago
  • 1.0.6                                ...           2 years ago
  • 1.0.5                                ...           2 years ago
  • 1.0.4                                ...           2 years ago
  • 1.0.3                                ...           2 years ago
  • 1.0.2                                ...           2 years ago
  • 1.0.0                                ...           2 years ago
Maintainers (1)
Downloads
Today 0
This Week 4
This Month 44
Last Day 0
Last Week 7
Last Month 50
Dependencies (3)
Dependents (0)
None

Copyright 2014 - 2016 © taobao.org |