2016-03-28 53 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);