redux undo/redo

Redux的文档中提供一个可以做undo/redo的解决办法,实际是有previous,current,prew的对象,围绕这数据的压入和弹出来实现操作步骤的记忆,结合persist就可以实现更强大的记忆功能.今天的这个增强组件实际把这个功能给包装了一下,内部实现细节仍然没有变.只需要把reducer用这个增强组件包装一下就可以用了.

redux undo/redo

提示:你可以使用redux-undo-boilerplate来开始项目.

Installation

npm install --save redux-undo
复制代码

API

import undoable from 'redux-undo';
undoable(reducer)
undoable(reducer, config)
复制代码

让你的reducers变得可以重做

redux-undo是一个reducer增强组件,它提供了undoable函数,这个函数接收已经存在的reducer和配置对象,使用undo函数增强已经存在的reducer.

**注意:**如果在state.counter之前接入,你必须要在包装reducer之后接入state.coutner.present.

首先导入redux-undo

 // Redux utility functions 
import { combineReducers } from 'redux';
// redux-undo higher-order reducer 
import undoable from 'redux-undo';
复制代码

接着,添加undoable到你的reducer

combineReducers({
  counter: undoable(counter)
})
复制代码

配置项想这样传递

combineReducers({
  counter: undoable(counter, {
    limit: 10 // set a limit for the history 
  })
})
复制代码

历史API

使用reducer包装你的reducer想这样

 {
  past: [...pastStatesHere...],
  present: {...currentStateHere...},
  future: [...futureStatesHere...]
}
复制代码

现在你可以使用state.present获取当前的state 获取所有过去的state使用state.past.

Undo/Redo Actions

首先导入undo/redo action creators

import { ActionCreators } from 'redux-undo';
复制代码

然后就可以使用store.dispatch()和undo/redo action creators来执行undo/redo操作.

store.dispatch(ActionCreators.undo()) // undo the last action 
store.dispatch(ActionCreators.redo()) // redo the last action 
 
store.dispatch(ActionCreators.jumpToPast(index)) // jump to requested index in the past[] array 
store.dispatch(ActionCreators.jumpToFuture(index)) // jump to requested index in the future[] array 
复制代码

配置

配置对象传递给undoable()(值是默认值)

undoable(reducer, {
  limit: false, // set to a number to turn on a limit for the history 
 
  filter: () => true, // see `Filtering Actions` section 
 
  undoType: ActionTypes.UNDO, // define a custom action type for this undo action 
  redoType: ActionTypes.REDO, // define a custom action type for this redo action 
 
  jumpToPastType: ActionTypes.JUMP_TO_PAST, // define custom action type for this jumpToPast action 
  jumpToFutureType: ActionTypes.JUMP_TO_FUTURE, // define custom action type for this jumpToFuture action 
 
  initialState: undefined, // initial state (e.g. for loading) 
  initTypes: ['@@redux/INIT', '@@INIT'] // history will be (re)set upon init action type 
  initialHistory: { // initial history (e.g. for loading) 
    past: [],
    present: config.initialState,
    future: []
  },
 
  debug: false, // set to `true` to turn on debugging 
})
复制代码

过滤Actions

如果你不想包含每一步的action,可以传递一个函数到undoable

undoable(reducer, function filterActions(action, currentState, previousState) {
  return action.type === SOME_ACTION; // only add to history if action is SOME_ACTION只有some_action的action才能记录 
})
 
// or you could do... 
 
undoable(reducer, function filterState(action, currentState, previousState) {
  return currentState !== previousState; // only add to history if state changed只有state变化的才能记录重做 
})
复制代码

或者你可以使用distinctState,includeAction,excludeAction助手函数

import undoable, { distinctState, includeAction, excludeAction } from 'redux-undo';
复制代码

现在你可以使用助手函数了,相当简单

undoable(reducer, { filter: includeAction(SOME_ACTION) })
undoable(reducer, { filter: excludeAction(SOME_ACTION) })
 
// or you could do... 
 
undoable(reducer, { filter: distinctState() })
复制代码

甚至还支持数组

 undoable(reducer, { filter: includeAction([SOME_ACTION, SOME_OTHER_ACTION]) })
undoable(reducer, { filter: excludeAction([SOME_ACTION, SOME_OTHER_ACTION]) })
复制代码

有什么魔法?怎么工作的

Redux文档中的实现Undo历史的方案解释了redux-undo工作的具体细节.

转载于:https://juejin.im/post/5cbf3380e51d456e266d8951

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值