Reducer

  • The Reducer is a context-independent pure function. It receives the following parameters
    • T state
    • Action action
  • It mainly contains three aspects of information
    • Receive an "intent" and make a state modification.
    • If you want to modify the state, you need to create a new copy and modify it on the copy.
    • If the small state is modified, it will automatically trigger the copy of the main state's layers data, and then notify the components to refresh in a flattened manner.
  • Sample Code
  1. /// one style of writing
  2. String messageReducer(String msg, Action action) {
  3. if (action.type == 'shared') {
  4. return '$msg [shared]';
  5. }
  6. return msg;
  7. }
  8.  
  9. class MessageComponent extends Component<String> {
  10. MessageComponent(): super(
  11. view: buildMessageView,
  12. effect: buildEffect(),
  13. reducer: messageReducer,
  14. );
  15. }
  1. /// another style of writing
  2. Reducer<String> buildMessageReducer() {
  3. return asReducer(<Object, Reducer<String>>{
  4. 'shared': _shared,
  5. });
  6. }
  7.  
  8. String _shared(String msg, Action action) {
  9. return '$msg [shared]';
  10. }
  11.  
  12. class MessageComponent extends Component<String> {
  13. MessageComponent(): super(
  14. view: buildMessageView,
  15. effect: buildEffect(),
  16. reducer: buildMessageReducer(),
  17. );
  18. }

推荐的是第二种写法