文章出處

簡介

  通過包裝reducer,創建一個state History,保留歷史state,可以做退一步,進一步操作

 

1.install

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

 

2.API(包裝reducer,其中config參數為history配置)

import undoable from 'redux-undo';
undoable(reducer)
undoable(reducer, config)

 

  2.1 和 combineReducers 配合使用

combineReducers({
  counter: undoable(counter)
})

 

3.History API

  3.1 包裝后的renducers 變成了,可通過state.present (獲取當前), state.past (獲取過去)

{
  past: [...pastStatesHere...],
  present: {...currentStateHere...},
  future: [...futureStatesHere...]
}

 

4.發起撤銷重做 Undo/Redo Actions

store.dispatch(ActionCreators.undo()) // undo the last action 退一步
store.dispatch(ActionCreators.redo()) // redo the last action 進一步

store.dispatch(ActionCreators.jump(-2)) // undo 2 steps
store.dispatch(ActionCreators.jump(5)) // redo 5 steps

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

store.dispatch(ActionCreators.clearHistory()) // [beta only] Remove all items from past[] and future[] arrays

 

5.配置項config

undoable(reducer, {
  limit: false, // set to a number to turn on a limit for the history // 保存到歷史的數量

  filter: () => true, // see `Filtering Actions` section  //過濾一部分action,不記錄/記錄在history

  undoType: ActionTypes.UNDO, // define a custom action type for this undo action
  redoType: ActionTypes.REDO, // define a custom action type for this redo action

  jumpType: ActionTypes.JUMP, // define custom action type for this jump 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

  clearHistoryType: ActionTypes.CLEAR_HISTORY, // [beta only] define custom action type for this clearHistory action

  initTypes: ['@@redux-undo/INIT'] // history will be (re)set upon init action type

  debug: false, // set to `true` to turn on debugging

  neverSkipReducer: false, // prevent undoable from skipping the reducer on undo/redo
})

 

 

6.初始化,history,   (看這個了解到,其實就是把 app 的 state={ todos:[], visiFilter:'showAll' }  包裝一層,變成下面的形式

import { createStore } from 'redux';

const initialHistory = {
  past: [0, 1, 2, 3],
  present: 4,
  future: [5, 6, 7]
}

const store = createStore(undoable(counter), initialHistory);

 

  1.不初始化歷史,只定義當前

import { createStore } from 'redux';

const store = createStore(undoable(counter), {foo: 'bar'});

// will make the state look like this:
{
  past: [],
  present: {foo: 'bar'},
  future: []
}

 

7.Filtering Actions (通過config)

  7.1 用于過濾,記錄進History的state

  7.2 包含include, 排除exclude

import undoable, { includeAction, excludeAction } from 'redux-undo';

undoable(reducer, { filter: includeAction(SOME_ACTION) })
undoable(reducer, { filter: excludeAction(SOME_ACTION) })

// they even support Arrays:

undoable(reducer, { filter: includeAction([SOME_ACTION, SOME_OTHER_ACTION]) })
undoable(reducer, { filter: excludeAction([SOME_ACTION, SOME_OTHER_ACTION]) })

 

  7.3 加入更多邏輯過濾 Custom filters

undoable(reducer, {
  filter: function filterActions(action, currentState, previousHistory) {
    return action.type === SOME_ACTION; // only add to history if action is SOME_ACTION
  }
})

// The entire `history` state is available to your filter, so you can make
// decisions based on past or future states:

undoable(reducer, {
  filter: function filterState(action, currentState, previousHistory) {
    let { past, present, future } = previousHistory;
    return future.length === 0; // only add to history if future is empty
  }
})

 

  7.4 合并多個filter函數

import undoable, {combineFilters} from 'redux-undo'

function isActionSelfExcluded(action) {
  return action.wouldLikeToBeInHistory
}

function areWeRecording(action, state) {
  return state.recording
}

undoable(reducer, {
  filter: combineFilters(isActionSelfExcluded, areWeRecording)
})

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

import { ignoreActions } from 'redux-ignore'

ignoreActions(
  undoable(reducer),
  [IGNORED_ACTION, ANOTHER_IGNORED_ACTION]
)

// or define your own function:

ignoreActions(
  undoable(reducer),
  (action) => action.type === SOME_ACTION // only add to history if action is SOME_ACTION
)

 

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是做什么的,看到,請幫我解惑,在此謝過


文章列表


不含病毒。www.avast.com
arrow
arrow
    全站熱搜
    創作者介紹
    創作者 大師兄 的頭像
    大師兄

    IT工程師數位筆記本

    大師兄 發表在 痞客邦 留言(0) 人氣()