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. class MessageComponent extends Component<String> {
  9. MessageComponent(): super(
  10. view: buildMessageView,
  11. effect: buildEffect(),
  12. reducer: messageReducer,
  13. );
  14. }
  1. /// another style of writing
  2. Reducer<String> buildMessageReducer() {
  3. return asReducer(<Object, Reducer<String>>{
  4. 'shared': _shared,
  5. });
  6. }
  7. String _shared(String msg, Action action) {
  8. return '$msg [shared]';
  9. }
  10. class MessageComponent extends Component<String> {
  11. MessageComponent(): super(
  12. view: buildMessageView,
  13. effect: buildEffect(),
  14. reducer: buildMessageReducer(),
  15. );
  16. }

推荐的是第二种写法