简介

  通过包装reducer,创建一个state History,保留历史state,可以做退一步,进一步操作

1.install

  1. npm install --save redux-undo@beta
  2. import ReduxUndo from 'redux-undo';

2.API(包装reducer,其中config参数为history配置)

  1. import undoable from 'redux-undo';
  2. undoable(reducer)
  3. undoable(reducer, config)

  2.1 和 combineReducers 配合使用

  1. combineReducers({
  2. counter: undoable(counter)
  3. })

3.History API

  3.1 包装后的renducers 变成了,可通过state.present (获取当前), state.past (获取过去)

  1. {
  2. past: [...pastStatesHere...],
  3. present: {...currentStateHere...},
  4. future: [...futureStatesHere...]
  5. }

4.发起撤销重做 Undo/Redo Actions

  1. store.dispatch(ActionCreators.undo()) // undo the last action 退一步
  2. store.dispatch(ActionCreators.redo()) // redo the last action 进一步
  3.  
  4. store.dispatch(ActionCreators.jump(-)) // undo 2 steps
  5. store.dispatch(ActionCreators.jump()) // redo 5 steps
  6.  
  7. store.dispatch(ActionCreators.jumpToPast(index)) // jump to requested index in the past[] array
  8. store.dispatch(ActionCreators.jumpToFuture(index)) // jump to requested index in the future[] array
  9.  
  10. store.dispatch(ActionCreators.clearHistory()) // [beta only] Remove all items from past[] and future[] arrays

5.配置项config

  1. undoable(reducer, {
  2. limit: false, // set to a number to turn on a limit for the history // 保存到历史的数量
  3.  
  4. filter: () => true, // see `Filtering Actions` section  //过滤一部分action,不记录/记录在history
  5.  
  6. undoType: ActionTypes.UNDO, // define a custom action type for this undo action
  7. redoType: ActionTypes.REDO, // define a custom action type for this redo action
  8.  
  9. jumpType: ActionTypes.JUMP, // define custom action type for this jump action
  10.  
  11. jumpToPastType: ActionTypes.JUMP_TO_PAST, // define custom action type for this jumpToPast action
  12. jumpToFutureType: ActionTypes.JUMP_TO_FUTURE, // define custom action type for this jumpToFuture action
  13.  
  14. clearHistoryType: ActionTypes.CLEAR_HISTORY, // [beta only] define custom action type for this clearHistory action
  15.  
  16. initTypes: ['@@redux-undo/INIT'] // history will be (re)set upon init action type
  17.  
  18. debug: false, // set to `true` to turn on debugging
  19.  
  20. neverSkipReducer: false, // prevent undoable from skipping the reducer on undo/redo
  21. })

6.初始化,history,   (看这个了解到,其实就是把 app 的 state={ todos:[], visiFilter:'showAll' }  包装一层,变成下面的形式

  1. import { createStore } from 'redux';
  2.  
  3. const initialHistory = {
  4. past: [, , , ],
  5. present: ,
  6. future: [, , ]
  7. }
  8.  
  9. const store = createStore(undoable(counter), initialHistory);

  1.不初始化历史,只定义当前

  1. import { createStore } from 'redux';
  2.  
  3. const store = createStore(undoable(counter), {foo: 'bar'});
  4.  
  5. // will make the state look like this:
  6. {
  7. past: [],
  8. present: {foo: 'bar'},
  9. future: []
  10. }

7.Filtering Actions (通过config)

  7.1 用于过滤,记录进History的state

  7.2 包含include, 排除exclude

  1. import undoable, { includeAction, excludeAction } from 'redux-undo';
  2.  
  3. undoable(reducer, { filter: includeAction(SOME_ACTION) })
  4. undoable(reducer, { filter: excludeAction(SOME_ACTION) })
  5.  
  6. // they even support Arrays:
  7.  
  8. undoable(reducer, { filter: includeAction([SOME_ACTION, SOME_OTHER_ACTION]) })
  9. undoable(reducer, { filter: excludeAction([SOME_ACTION, SOME_OTHER_ACTION]) })

  7.3 加入更多逻辑过滤 Custom filters

  1. undoable(reducer, {
  2. filter: function filterActions(action, currentState, previousHistory) {
  3. return action.type === SOME_ACTION; // only add to history if action is SOME_ACTION
  4. }
  5. })
  6.  
  7. // The entire `history` state is available to your filter, so you can make
  8. // decisions based on past or future states:
  9.  
  10. undoable(reducer, {
  11. filter: function filterState(action, currentState, previousHistory) {
  12. let { past, present, future } = previousHistory;
  13. return future.length === ; // only add to history if future is empty
  14. }
  15. })

  7.4 合并多个filter函数

  1. import undoable, {combineFilters} from 'redux-undo'
  2.  
  3. function isActionSelfExcluded(action) {
  4. return action.wouldLikeToBeInHistory
  5. }
  6.  
  7. function areWeRecording(action, state) {
  8. return state.recording
  9. }
  10.  
  11. undoable(reducer, {
  12. filter: combineFilters(isActionSelfExcluded, areWeRecording)
  13. })

  7.5 忽略指定的action---- Ignoring Actions

  1. import { ignoreActions } from 'redux-ignore'
  2.  
  3. ignoreActions(
  4. undoable(reducer),
  5. [IGNORED_ACTION, ANOTHER_IGNORED_ACTION]
  6. )
  7.  
  8. // or define your own function:
  9.  
  10. ignoreActions(
  11. undoable(reducer),
  12. (action) => action.type === SOME_ACTION // only add to history if action is SOME_ACTION
  13. )

Note: Since beta4, only actions resulting in a new state are recorded. This means the (now deprecated) distinctState()filter is auto-applied.

这句话的意思应该是:从beta4 开始,只有触发 action 产生的 state 才会记录在 history, 有redo/undo 产生的是不会被记录的,可以使用distinctState() 兼容

然而我并不明白istinctState是做什么的,看到,请帮我解惑,在此谢过

redux-undo的更多相关文章

  1. react+redux教程(四)undo、devtools、router

    上节课,我们介绍了一些es6的新语法:react+redux教程(三)reduce().filter().map().some().every()....展开属性 今天我们通过解读redux-undo ...

  2. Unity 之 Redux 模式(第一篇)—— 人物移动

    作者:软件猫 日期:2016年12月6日 转载请注明出处:http://www.cnblogs.com/softcat/p/6135195.html 在朋友的怂恿下,终于开始学 Unity 了,于是有 ...

  3. 总结下Redux

    Redux 和 React 没有直接关系,它瞄准的目标是应用状态管理. 核心概念是 Map/Reduce 中的 Reduce.且 Reducer 的执行是同步,产生的 State 是 Immutabl ...

  4. Redux 核心概念

    http://gaearon.github.io/redux/index.html ,文档在 http://rackt.github.io/redux/index.html .本文不是官方文档的翻译. ...

  5. Redux应用多人协作的思路和实现

    先上Demo动图,效果如下: 基本思路 由于redux更改数据是dispatch(action),所以很自然而然想到以action作为基本单位在服务端和客户端进行传送,在客户端和服务端用数组来存放ac ...

  6. Redux/Mobx/Akita/Vuex对比 - 选择更适合低代码场景的状态管理方案

    近期准备开发一个数据分析 SDK,定位是作为数据中台向外输出数据分析能力的载体,前端的功能表现类似低代码平台的各种拖拉拽.作为中台能力的载体,SDK 未来很大概率会需要支持多种视图层框架,比如Vue2 ...

  7. RxJS + Redux + React = Amazing!(译一)

    今天,我将Youtube上的<RxJS + Redux + React = Amazing!>翻译(+机译)了下来,以供国内的同学学习,英文听力好的同学可以直接看原版视频: https:/ ...

  8. 通过一个demo了解Redux

    TodoList小demo 效果展示 项目地址 (单向)数据流 数据流是我们的行为与响应的抽象:使用数据流能帮我们明确了行为对应的响应,这和react的状态可预测的思想是不谋而合的. 常见的数据流框架 ...

  9. RxJS + Redux + React = Amazing!(译二)

    今天,我将Youtube上的<RxJS + Redux + React = Amazing!>的后半部分翻译(+机译)了下来,以供国内的同学学习,英文听力好的同学可以直接看原版视频: ht ...

  10. redux学习

    redux学习: 1.应用只有一个store,用于保存整个应用的所有的状态数据信息,即state,一个state对应一个页面的所需信息 注意:他只负责保存state,接收action, 从store. ...

随机推荐

  1. 【疯狂造轮子-iOS】JSON转Model系列之二

    [疯狂造轮子-iOS]JSON转Model系列之二 本文转载请注明出处 —— polobymulberry-博客园 1. 前言 上一篇<[疯狂造轮子-iOS]JSON转Model系列之一> ...

  2. ASP.NET Core HTTP 管道中的那些事儿

    前言 马上2016年就要过去了,时间可是真快啊. 上次写完 Identity 系列之后,反响还不错,所以本来打算写一个 ASP.NET Core 中间件系列的,但是中间遇到了很多事情.首先是 NPOI ...

  3. Asp.net MVC 传递数据 从前台到后台,包括单个对象,多个对象,集合

    今天为大家分享下 Asp.net MVC 将数据从前台传递到后台的几种方式. 环境:VS2013,MVC5.0框架 1.基本数据类型 我们常见有传递 int, string, bool, double ...

  4. Beanstalkd一个高性能分布式内存队列系统

    高性能离不开异步,异步离不开队列,内部是Producer-Consumer模型的原理. 设计中的核心概念: job:一个需要异步处理的任务,是beanstalkd中得基本单元,需要放在一个tube中: ...

  5. 在 SAE 上部署 ThinkPHP 5.0 RC4

    缘起 SAE 和其他的平台有些不同,不能在服务器上运行 Composer 来安装各种包,必须把源码都提交上去.一般的做法,可能是直接把源码的所有文件复制到目录中,添加到版本库.不过,这样就失去了与上游 ...

  6. android键盘

    在应用的开发过程中有不少的情况下会用到自定义键盘,例如支付宝的支付密码的输入,以及类似的场景.android系统给开发者们提供了系统键盘,KeyboardView,其实并不复杂,只是有些开发者不知道罢 ...

  7. 关于font-family

    在设置页面字体的时候,你会发现在 font-family 属性中会设置好多个字体,想看懂它们都是什么字体吗?不好意思,我不是搞设计的,我也不知道.那么,现在写的东西,只是对于一个前端人员来说,要了解的 ...

  8. 微信小程序教程汇总

    目前市面上在内测期间出来的一些实战类教程还是很不错的,主要还是去快速学习小程序开发的整体流程,一个组件一个组件的讲的很可能微信小程序一升级,这个组件就变了,事实本就如此,谁让现在是内测呢.我们不怕,下 ...

  9. SharePoint 2013: A feature with ID has already been installed in this farm

    使用Visual Studio 2013创建一个可视web 部件,当右击项目选择"部署"时报错: "Error occurred in deployment step ' ...

  10. iOS--->微信支付小结

    iOS--->微信支付小结 说起支付,除了支付宝支付之外,微信支付也是我们三方支付中最重要的方式之一,承接上面总结的支付宝,接下来把微信支付也总结了一下 ***那么首先还是由公司去创建并申请使用 ...