首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >当我由于某种原因组合减速机时,我的减速机没有附着在商店里,那是怎么回事?

当我由于某种原因组合减速机时,我的减速机没有附着在商店里,那是怎么回事?
EN

Stack Overflow用户
提问于 2020-07-31 01:55:45
回答 1查看 280关注 0票数 0

见对问题的形象解释

问题:

当进口时,auth减速机在全球范围内无法被识别&试图与其他减速机相结合。其他减速机,如警报配置文件 & 帖子已经被全球商店所认可。

8月减速机:

Github:https://github.com/DariusRain/Pluto/blob/master/client/src/redux/modules/auth.js

代码语言:javascript
复制
import api from "../../utils/api";
import { setAlert } from "./alert";

// Action Types
export const REGISTER_SUCCESS = "PLUTO/AUTH/REGISTER_SUCCESS";
export const REGISTER_FAIL = "PLUTO/AUTH/REGISTER_FAIL";
export const LOGIN_SUCCESS = "PLUTO/AUTH/LOGIN_SUCCESS";
export const LOGIN_FAIL = "PLUTO/AUTH/LOGIN_FAIL";
export const USER_LOADED = "PLUTO/AUTH/USER_LOADED";
export const AUTH_ERROR = "PLUTO/AUTH/ERROR";
export const LOGOUT = "PLUTO/AUTH/LOGOUT";
export const ACCOUNT_DELETED = "PLUTO/AUTH/ACCOUNT_DELETED";

// Reducer
const initialState = {
  token: localStorage.getItem("token"),
  isAuthenticated: null,
  loading: true,
  user: null,
};

export default (state = initialState, { type, payload }) => {
  switch (type) {
    case USER_LOADED:
      return {
        ...state,
        isAuthenticated: true,
        loading: false,
        user: payload,
      };
    case REGISTER_SUCCESS:
      return {
        ...state,
        ...payload,
        isAuthenticated: true,
        loading: false,
      };
    case LOGIN_SUCCESS:
      return {
        ...state,
        ...payload,
        isAuthenticated: true,
        loading: false,
      };
    case ACCOUNT_DELETED:
      return {
        ...state,
        token: null,
        isAuthenticated: false,
        loading: false,
        user: null,
      };
    case REGISTER_FAIL:
    case AUTH_ERROR:
    case LOGOUT:
      return {
        ...state,
        token: null,
        isAuthenticated: false,
        loading: false,
        user: null,
      };
    default:
      return state;
  }
};


// Action Creators
export const loadUser = () => async (dispatch) => {
  try {
    const res = await api.get("/auth");
    dispatch({
      type: USER_LOADED,
      payload: res.data,
    });
  } catch (err) {
    dispatch({
      type: AUTH_ERROR,
    });
  }
};
// Register User
export const register = (formData) => async (dispatch) => {
  try {
    const res = await api.post("/users", formData);
    dispatch({
      type: REGISTER_SUCCESS,
      payload: res.data,
    });
    dispatch(loadUser());
  } catch (err) {
    const errors = err.response.data.errors;
    if (errors) {
      errors.forEach((error) => dispatch(setAlert(error.msg, "danger")));
    }
    dispatch({
      type: REGISTER_FAIL,
    });
  }
};

// Login User
export const login = (email, password) => async (dispatch) => {
  try {
    const res = await api.post("/auth", { email, password });
    dispatch({
      type: LOGIN_SUCCESS,
      payload: res.data,
    });
    dispatch(loadUser());
  } catch (err) {
    const errors = err.response.data.errors;
    if (errors) {
      errors.forEach((error) => dispatch(setAlert(error.msg, "danger")));
    }
    dispatch({
      type: LOGIN_FAIL,
    });
  }
};
// Logout
export const logout = () => async (dispatch) => dispatch({ type: LOGOUT });

联合收割机

Github:https://github.com/DariusRain/Pluto/blob/master/client/src/redux/modules/index.js

代码语言:javascript
复制
import {
  combineReducers
} from "redux";
import alert from "./alert";
import post from "./post";
import profile from "./profile";
import auth from "./auth";

export default combineReducers({
  alert,
  profile,
  post,
  auth,
});

商店

Github:https://github.com/DariusRain/Pluto/blob/master/client/src/redux/index.js

代码语言:javascript
复制
import { createStore, applyMiddleware } from 'redux';
import { composeWithDevTools } from 'redux-devtools-extension';
import thunk from 'redux-thunk';
import rootReducer from './modules';
import setAuthToken from '../utils/setAuthToken';

const initialState = {};

const middleware = [thunk];

const store = createStore(
  rootReducer,
  initialState,
  composeWithDevTools(applyMiddleware(...middleware))
);

let currentState = store.getState();

store.subscribe(() => {
  let previousState = currentState;
  currentState = store.getState();
  if (previousState.auth.token !== currentState.auth.token) {
    const token = currentState.auth.token;
    setAuthToken(token);
  }
});

export default store;

储存库:

冥王星:https://github.com/DariusRain/Pluto

帮助感激!-DariusRain

EN

回答 1

Stack Overflow用户

发布于 2020-07-31 07:38:22

您有一个循环依赖问题。初始化存储时导入它:

  1. App.js ->从‘./redux’导入存储;
  2. 存储初始化: ( a)来自“././utils/ api”的redux/ -> /auth.js导入api ( b) utils/api.js从'../redux‘->导入存储,在这里您会崩溃

我建议将减速器和动作创建者分割成单独的文件

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/63184729

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档