Reputation: 130
I use a series of data as follows:
[
{
name: 'name1',
background:'red',
child:[
{
name:'',
id:'',
color:'',
text:'',
border:''
},
{
name:'',
id:'',
color:'',
text:'',
border:''
}
]
},
{
name: 'name2',
background:'red',
child:[
{
name:'',
id:'',
color:'',
text:'',
border:''
},
{
name:'',
id:'',
color:'',
text:'',
border:''
}
]
}
]
I'm going to save all the changes to another variable, and I used a deep copy to do that, but when I log in, the variables are the same.I need to children all the children changes too.
I wrote it in Reducers
const Reducers =(state = initialState, action) => {
switch (action.type) {
case NEW_OBJECTS_PAST:
const OldPast = JSON.parse(JSON.stringify(state.past))
const newDate = JSON.parse(JSON.stringify(state.present))
// const newDate = _.cloneDeep(state.present);
const newPast = [
OldPast,
newDate
];
return {
...state,
past : _.cloneDeep(newPast) ,
}
case OBJECTS:
return {
...state,
present: action.objects,
// future:[]
}
Do you suggest another way to build undo in REACT and REDUX ? I tried the libraries available for this, but got no answer.
Upvotes: 1
Views: 120
Reputation: 5371
First two remarks :
To implement undo, I'm not sure what your actions are supposed to mean but you can do it as follows
state.present
(the current state) and state.past
(an array of past states)state.present
at the end of state.past
and compute the new state.present
state.past
and put it in state.present
.In your code I can't see any undo action, and you're also building nested arrays because of new Past = [oldPast, new Date]
, you most likely meant to spread oldPast
.
Upvotes: 1