Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // App
- class App extends React.Component {
- componentDidMount() {
- store.dispatch(loadUser());
- }
- render() {
- return (
- <Provider store={store}>
- <div className="App">
- <AppNavbar />
- <Container>
- <ItemModal />
- <ShoppingList />
- </Container>
- </div>
- </Provider>
- );
- }
- }
- // store
- const initialState = {};
- const middleware = [thunk];
- const store = createStore(
- rootReducer,
- initialState,
- compose(
- applyMiddleware(...middleware),
- window.__REDUX_DEVTOOLS_EXTENSION__ && window.__REDUX_DEVTOOLS_EXTENSION__()
- )
- );
- // action
- export const loadUser = () => (dispatch, getState) => {
- dispatch({ type: USER_LOADING });
- axios
- .get("/api/auth/user", tokenConfig)
- .then(res =>
- dispatch({
- type: USER_LOADED,
- payload: res.data
- })
- )
- .catch(err => {
- const { data, status } = err.response;
- dispatch(returnErrors(data, status));
- dispatch({
- type: AUTH_ERROR
- });
- });
- };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement