Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does Redux have a built-in way to undo actions?

Tags:

redux

I'm building an app where actions are performed as the user scrolls down. It would be nice if I could undo those actions as the user scrolls up again, basically turning scrolling into a way to browse through the time line of actions.

Is there a built-in way in Redux to do this? Or would I have to write middleware for this?

like image 667
Vincent Avatar asked Sep 11 '15 14:09

Vincent


People also ask

How do you undo an action in Redux?

To "undo" an action, you just replace the application state with one of the saved states.

How do you implement undo/redo in react?

To implement undo/redo functionality with React you don't need to use Konva 's serialization and deserealization methods. You just need to save a history of all the state changes within your app. There are many ways to do this. It may be simpler do to that if you use immutable structures.

What is the difference between reducer and action in Redux?

Reducers: As we already know, actions only tell what to do, but they don't tell how to do, so reducers are the pure functions that take the current state and action and return the new state and tell the store how to do.

How does action work in Redux?

Actions are the only source of information for the store as per Redux official documentation. It carries a payload of information from your application to store. const ITEMS_REQUEST = 'ITEMS_REQUEST'; Apart from this type attribute, the structure of an action object is totally up to the developer.


2 Answers

I believe the idea is not so much "undo" as much as save a reference to the entire state tree each time an action passes through redux.

You would have a history stack made up of the application state at various times.

let history = [state1, state2, state3]

// some action happens

let history = [state1, state2, state3, state4]

// some action happens

let history = [state1, state2, state3, state4, state5]

// undo an action

let history = [state1, state2, state3, state4]

state = state4

To "undo" an action, you just replace the application state with one of the saved states.

This can be made efficient with data structures that support structural sharing, but in development we don't really need to consider resource constraints too much anyway.

like image 96
Qiming Avatar answered Oct 20 '22 15:10

Qiming


Is there a built-in way in Redux to do this? Or would I have to write middleware for this?

Middleware sounds like the wrong idea in this case because this is purely state management concern. Instead you can write a function that takes a reducer and returns a reducer, “enhancing” it with action history tracking along the way.

I outlined this approach in this answer, and it's similar to how redux-undo works, except that instead of storing the state, you can store actions. (Depends on the tradeoffs you want to make, and whether it's important to be able to “cancel” actions in a different order than they happened.)

like image 30
Dan Abramov Avatar answered Oct 20 '22 16:10

Dan Abramov