我正在构建一个React / Redux应用程序。设置我的商店后,状态恢复为“未定义”。商店中的初始状态为空,但减速器均携带初始状态。 react开发工具可以工作,但状态不会改变。
// App.js
function App() {
return (
<Provider store={store}>
<Router>
<div>
<Navbar />
<Switch>
<Route exact path='/' component={Home} />
<Route exact path='/subscriptions' component={Subscriptions} />
<Route exact path='/login' component={Login} />
<Route exact path='/register' component={Register} />
<Route exact path='/dashboard' component={Dashboard} />
</Switch>
<Footer />
</div>
</Router>
</Provider>
);
}
export default App;
// Store.js
import { createStore, applyMiddleware, compose } from 'redux'; // Compose is brought in so that we can combine the reducer with what is required by chrome to add the Redux extension
import thunk from 'redux-thunk'; // Middleware
import rootReducer from './reducers/index'; // Step 1. This is where we will combine all other reducers and be able to bring it into one file as clean as possible
const initialState = {}
// Step 2
// Create the store
const store = createStore(
rootReducer,
initialState,
compose(applyMiddleware(thunk))
);
// Exporting the store to bring into main App.js file and be able to use across the entired app.
export default store;
Reducers / index.js
import { combineReducers } from 'redux';
import authReducer from './authReducer';
import errorReducer from './errorReducer';
export default combineReducers({
auth: authReducer,
errors: errorReducer
});
Actions / types.js
export const GET_ERRORS = 'GET_ERRORS';
export const SET_CURRENT_USER = 'SET_CURRENT_USER';