A plugin of dva to support undo redo based on Immer.
$ npm install dva-immer-undo-redo --save
import createUndoRedo from 'dva-immer-undo-redo';
const app = dva();
app.use(createUndoRedo(options));
options.namespace
: property key on global state, type String, Defaulttimeline
options.include
: namespaces that you want to include in the timeline. Default[]
options.limit
: the max times to undo or redo, Default1024
dispatch({ type: 'timeline/undo' });
dispatch({ type: 'timeline/redo' });
dispatch({ type: 'timeline/clear' });
// state of count's model
{
count: 0,
}
// add reducer
add(state) {
state.count += 1;
}
dispatch({ type: 'count/add' }); // { count: 1 }
dispatch({ type: 'count/add' }); // { count: 2 }
dispatch({ type: 'count/add', replace: true }); // { count: 3 }
dispatch({ type: 'timeline/undo' }); // { count: 1 }
dispatch({ type: 'timeline/redo' }); // { count: 3 }
// state of count's model
{
count: 0,
}
// add reducer
add(state) {
state.count += 1;
}
dispatch({ type: 'count/add' }); // { count: 1 }
dispatch({ type: 'count/add' }); // { count: 2 }
dispatch({ type: 'count/add', clear: true }); // { canRedo: false, canUndo: false }
timeline: {
canRedo: false,
canUndo: false,
undoCount: 0,
redoCount: 0,
}