无法设置redux-localstorage以使用现有商店

时间:2016-03-28 10:12:50

标签: local-storage react-native redux

我似乎无法在redux-localstorage上设置react-native模块。我收到一条错误消息:

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中存储 1 对象,这是用户对象。哪个将保留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或者我是否在正确的轨道上并犯了一个小错误。

1 个答案:

答案 0 :(得分:1)

正如Josh Buchea所指出的,你不能在React Native上使用localStorage,所以AsyncStorage就是出路。

redux-localstorage已经有adapter

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

const storage = adapter(AsyncStorage);