Search code examples
reactjsreact-nativeredux

How to get previous state in redux


i want to set state to his previous value on click on button........... to change redux-state to previous redux-state

reducer file

export var key = (state = 0, action) => {
    if (action.type === 'changeKey') {
        return action.playload
    }
    else {
        return state
    }
}

action file

export var keyAction = (name) => {
    return {
        type: 'changeKey',
        playload: name,
    }
}

3 file

<Text onPress={()=>{
//what i need to write here ???
}}> Set redux-state to previous value of redux-state</Text>

Solution

  • I make array list- every time i add key it will add the value on previous array

    When i want to set redux-state to previous state i just delete last item of an array, and set redux-state to last item of that array

    var list = [0]
    
    export var keyAction = (name) => {
        list = [...list, name]
        return {
            type: 'changeKey',
            playload: name,
        }
    }
    export var keyBackAction = () => {
        list.splice(-1)
        return {
            type: 'getBack',
            playload: list[list.length - 1],
        }
    }