2016-03-28 49 views
0

我似乎无法设置redux-localstorage模块react-native。我得到一个错误消息说:难以设置的REDX本地存储与现有的商店

TypeError: (0, _reduxLocalstorage.mergePersistedState) is not a function. (In '(0, _reduxLocalstorage.mergePersistedState)()', '(0, _reduxLocalstorage.mergePersistedState)' is undefined) 

这里是我的configStore.jsx文件的样子。这是我最初使用thunk中间件安装我的redux商店的地方。网上没有很多示例显示如何设置redux-localstorage软件包!我不是100%确定它是如何工作的。我只需要在localstorage内部存储对象,这是用户对象。其中将持有api authentication key + user details。如果任何人都能指出我的方向,那将是惊人的!

const local_storage_reducer = compose(
    mergePersistedState() 
)(reducers); 

const storage = compose(
    filter('user'), 
    adapter(window.localStorage) 
) 
const createPersistentStore = compose(
    applyMiddleware(thunk), 
    persistState(storage), 
)(createStore); 


const createStoreWithMiddleware = compose(
    applyMiddleware(thunk), 
    persistState(storage, 'my-storage-key') 
)(createStore); 

export default function configureStore() { 
    return createStoreWithMiddleware(combineReducers(reducers)); 
} 

export default function configurePersistentStore() { 
    return createPersistentStore(reducers); 
} 

以下是我如何通过provider组件将商店注入到我的组件中。

const store = configureStore(); 
const persistentStore = configurePersistentStore(); 
<Provider store={[store,persistentStore] }> 
     .... Router stuff here .... 

我不知道这是怎么远程设置你的redux-localstorage还是我在正确的轨道上,并取得了一个小错误。

+0

window.localStorage不存在阵营本土。看[AsyncStorage](https://facebook.github.io/react-native/docs/asyncstorage.html)。 –

回答

1

由于Josh Buchea指出你不能在React Native上使用localStorage,所以AsyncStorage是出路。

终极版,localStorage的已经有对于一个adapter

import {AsyncStorage} from 'react-native'; 
import adapter from 'redux-localstorage/lib/adapters/AsyncStorage'; 

const storage = adapter(AsyncStorage);