Home > Net >  How to get previous state in redux
How to get previous state in redux

Time:11-03

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>

CodePudding user response:

You can use redux-undo (https://github.com/omnidan/redux-undo) or implement your own custom logic following this documentation https://redux.js.org/usage/implementing-undo-history

CodePudding user response:

You should dispatch your redux action

import { useDispatch } from 'react-redux'


const FC = () => {
    const dispatch = useDispatch()

    return (
        <Text onPress={() => dispatch(keyAction("stackoverflow"))}>
            Set redux-state to previous value of redux-state
        </Text>
    )
}

You also wrote playload instead of payload

  • Related