我正在尝试在已经使用redux的react-native项目中设置redux-persist。我不能让它工作。
我遵循了这篇文章:https://medium.com/differential/building-offline-first-react-native-apps-b958acac0009#.vmshi0cxk
这是我在app.js中的设置:
import React, {Component} from 'react';
import { Navigator, View, StyleSheet } from 'react-native';
import { createStore, applyMiddleware, combineReducers } from 'redux';
import { Provider } from 'react-redux';
import thunk from 'redux-thunk';
import { persistStore, autoRehydrate } from 'redux-persist';
import * as reducers from '../reducers';
const createStoreWithMiddleware = applyMiddleware(thunk)(createStore);
configureStore = (onComplete) => {
const store = autoRehydrate()(createStoreWithMiddleware)(reducers);
persistStore(store, { storage: AsyncStorage }, onComplete);
return store;
};
App的开头(在同一个文件中):
class App extends Component {
constructor(props) {
super(props);
this.state = {
isLoading: true,
store: configureStore(() => this.setState({ isLoading: false })),
};
}
render() {
if (this.state.isLoading) return null;
....
}
}
尝试此操作时,我将“reducer is not function”视为错误。
在/ reducers文件夹中我有index.js:
import recordReducer from './recordReducer';
export {
recordReducer
};
并在recordReducer.js中:
import * as types from '../actions/actionTypes';
const initialState = {};
export default function recordReducer(state = initialState, action = {}) {
switch (action.type) {
case types.SAVE_RECORDING_SUCCESS:
return {
...state,
recordings: state.recordings ? state.recordings.concat([action.recording]) : [action.recording]
};
default:
return state;
}
}