【问题标题】:React Native - How to store redux state in asyncstorageReact Native - 如何在异步存储中存储 redux 状态
【发布时间】:2019-07-23 10:42:45
【问题描述】:

实际上,我是 redux 概念的新手。我正在开发一些登录应用程序,我必须将用户信息存储在 redux 状态。我在整个应用程序中使用此用户信息。但是在移动应用程序中,如果我们终止应用程序并重新启动用户信息,则应该从 redux 状态获取 obj。为此,我想使用 redux 将此对象存储在异步存储中。谁能帮帮我

代码:

    const initialState = {
        userinfo : {}
    }

    const UserInfoReducer = (state =  initialState,action) => {

        switch(action.type){

            case 'SAVE_TOKEN' : 
             return {state, userinfo: action.payload}

            case 'RETRIEVE_TOKEN':
              return state
        }
            return state

    }
    export default UserInfoReducer;


  saveTokenMethod = async() => {
       var tokenInfo = {
           token : this.state.username
       } 
       this.props.saveToken(tokenInfo)
    }

const mapDispatchToProps = (dispatch) => {
    return {
        saveToken : (tokenInfo) => dispatch({type : 'SAVE_TOKEN',payload : tokenInfo})
    }
}

【问题讨论】:

标签: react-native redux react-redux


【解决方案1】:

您可以使用redux-persist 来处理设备的持久状态,因为您使用的是 Redux,它将使用 AsyncStorage 作为默认值来存储状态。你也可以使用redux-persist-transform-filters 来选择应该持久化哪些redux,哪些不应该。

redux-persist

redux-persist-transform-filters

【讨论】:

    【解决方案2】:

    在应用启动时检索存储数据

    follow this post

    【讨论】:

    • 在这个我必须在每个组件中添加 appstate 监听器
    【解决方案3】:

    试试这个

    First create a reducerfile Index(like index.js)
    
    import { combineReducers } from 'redux';
    import tokenReducer from ./savetoken.js;
    
    export default combineReducers({
       mytoken:tokenReducer 
    })
    
    
    
    
    
    1) Reducer function(savetoken.js)
    
       const initialState = {
            userinfo : {}
        }
    
    export default (state = initialState,action) => {
    
            switch(action.type){
    
                case 'SAVE_TOKEN' : 
                 return {state, userinfo: action.payload}
    
                case 'RETRIEVE_TOKEN':
                  return state
            }
                return state
    
     }
    
    }
    
    2) Action Function
    
    export const saveTokenMethod = (value) => {
    
    return{
            type:'SAVE_TOKEN',
            payload:value
        }
    
    } 
    
    3) LoginPage (Saving token part)..let take loginpage.js
    
      import * as actions from '../../../../actions';(Give your action page path here)
    
    Class LoginPage{
       .....
    
     this.props.saveTokenMethod(token);
     var user_token = this.props.mytoken;
     AsyncStorage.setItem('logintoken',user_token );
    
    
    }
    
    const mapStateToProps = state => {
    
    return{
       mytoken:state.mytoken
    }
    
    }
    
    export default connect(mapStateToProps,actions})(LoginPage)
    

    【讨论】:

      【解决方案4】:

      转换你的 redux 状态 JSON.stringify(obj);并存储在 异步存储

      【讨论】:

      • 我在 asyncstorage reducer 中做不到
      猜你喜欢
      • 2016-04-21
      • 2016-11-14
      • 1970-01-01
      • 2020-05-13
      • 2021-03-17
      • 2019-08-17
      • 2021-12-13
      • 2019-10-25
      • 2021-03-18
      相关资源
      最近更新 更多