37
A Higher-Order Component that implements a reducer a la Rematch.js to control co...
source link: https://www.tuicool.com/articles/hit/QJ3qY3z
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
with-rematch
A HoC that implement reducer a la Rematch
Inpired By Rematch.js ♡
Instalation
npm install -g with-rematch or yarn add with-rematch
Getting Started
Step 1: Model
const model = { state: 0, reducers: { increment(state, payload, props) { console.log(state); // the state console.log(payload); // the payload passed via action console.log(props); // component props return state + (payload || 1); }, decrement(state, payload, props) { return state - (payload || 1); } }, effects: actions => ({ async asyncIncrement(payload, state, props) { await new Promise(resolve => setTimeout(resolve, 1000)); actions.increment(); } }), lifecycle: { componentDidMount() { console.log('componentDidMount', this); } } };
Understanding models is as simple as answering a few questions:
- What is my initial state? state
- How do I change the state? reducers
- How do I handle async actions? effects with async/await
Step 2: HoC Props (state, actions)
Actions is how we trigger reducers & effects in your models. actions
props standardizes your actions without the need for writing action types or action creators.
// reducers actions.increment(); actions.increment(5); actions.decrement(2); // effects actions.asyncIncrement(10);
const Counter = ({state, actions}) => ( <div> Value: {state} <button onClick={actions.increment}>INCREMENT</Button> <button onClick={actions.decrement}>DENCREMENT</button> <button onClick={actions.asyncIncrement}>ASYNC INCREMENT</button> <button onClick={() => actions.increment(5)}>INCREMENT 5</Button> </View> ); export default withRematch(model)(Counter);
Step 3: Lifecycle
TODO
Complete Example
import withRematch from 'with-rematch'; const model = { state: 0, reducers: { increment(state, payload, props) { console.log(state); // the state console.log(payload); // the payload passed via action console.log(props); // component props return state + (payload || 1); }, decrement(state, payload, props) { return state - (payload || 1); } }, effects: actions => ({ async asyncIncrement(payload, state, props) { await new Promise(resolve => setTimeout(resolve, 1000)); actions.increment(); } }), lifecycle: { componentDidMount() { console.log('componentDidMount', this); } } }; const Counter = ({state, actions}) => ( <div> Value: {state} <button onClick={actions.increment}>INCREMENT</Button> <button onClick={actions.decrement}>DENCREMENT</button> <button onClick={actions.asyncIncrement}>ASYNC INCREMENT</button> <button onClick={() => actions.increment(5)}>INCREMENT 5</Button> </View> ); export default withRematch(model)(Counter);
Recommend
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK