Features
- Simplest - Same syntax as a class component
- Only 2 API -
setStore()
and withStore()
- Async model - Code splitting support for models
- Auto loading - Auto loading state for async actions
Install
yarn add retalk
Usage
Model syntax is like a React class component, just without lifecycle methods.
import { setStore, withStore, Provider } from 'retalk';
class CounterModel {
state = {
count: 0,
};
add() {
const { count } = this.state;
this.setState({ count: ++count });
this.addAsync();
}
async addAsync() {
await new Promise((resolve) => setTimeout(resolve, 1000));
const { count } = this.state;
this.setState({ count: ++count });
}
}
const Counter = withStore({
counter: ['count', 'add', 'addAsync'],
})((props) => {
const { count, add, addAsync } = props;
return (
<div>
<p>{count}</p>
<button onClick={add}>+</button>
<button onClick={addAsync}>+ ⏳{addAsync.loading && '...'}</button>
</div>
);
});
const store = setStore({ counter: CounterModel });
const App = () => (
<Provider store={store}>
<Counter />
</Provider>
);
Demo

API
1. setStore()
setStore(models, middleware)
const store = setStore(
{
home: HomeModel,
counter: CounterModel,
},
[logger, crashReporter]
);
Pass models
and middleware
(both are optional), Set up 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.
2. withStore()
withStore(...modelNames)(Component)
Eject state and actions of one or more models, to the props of a component. 3 ways to use it:
const Wrapper = withStore('home', 'counter')(Counter);
const Wrapper = withStore({
home: ['name', 'setName'],
counter: ['count', 'add', 'addAsync'],
})(Counter);
const Wrapper = withStore(mapStateToProps, mapDispatchToProps)(Counter);
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 Wrapper = loadable(async () => {
const [{ default: Counter }, { default: CounterModel }] = await Promise.all([
import('./Counter/index.jsx'),
import('./Counter/Model.js'),
]);
store.add({ counter: CounterModel });
return (props) => <Counter {...props} />;
});
License
MIT © nanxiaobei
FUTAKE
Try FUTAKE in WeChat. A mini app for your inspiration moments. 🌈
