reducer分析
redux文件
redux
第一个文件: actions.js
export const AUTH = 'auth/AUTH';
export const LOGIN = 'auth/LOGIN';
export const LOGIN_SUCCESS = 'auth/LOGIN_SUCCESS';
export const LOGIN_FAIL = 'auth/LOGIN_FAIL';
export const COMMON_SUCCESS = 'auth/COMMON_SUCCESS';
export const COMMON_ERROR = 'auth/COMMON_ERROR';
第一个reducer auth.js
import {
AUTH,
LOGIN,
LOGIN_SUCCESS,
LOGIN_FAIL,
COMMON_SUCCESS,
COMMON_ERROR
} from './actions'
const initialState = {
loaded: false
};
export default function reducer(state = initialState, action = {}) {
switch (action.type) {
case AUTH:
return {
...state,
auth: action.data
};
case LOGIN:
return {
...state,
requesting: true,
requested: false
};
case LOGIN_SUCCESS:
return {
...state,
requesting: false,
requested: true,
loginError: action.res.data.message
};
case COMMON_ERROR:
return {
...state,
requesting: false,
requested: true,
loginError: action.error
};
case COMMON_SUCCESS:
return {
...state,
requesting: false,
requested: true
};
case LOGIN_FAIL:
return {
...state,
requesting: false,
requested: true,
loginError: action.error
};
default:
return state;
}
}
// vanish预审核页面发送 自动发送失败 动用这个接口
export function sendVanishMessage(option) {
return {
types: [COMMON_SUCCESS, COMMON_ERROR],
promise: axios.get(`/service_mana/`, {params: option})
};
}
// 获取验证码
export function getCode(option) {
return {
types: [COMMON_SUCCESS, COMMON_ERROR],
promise: axios.get(`/service`, {params: option})
};
}
第二个reducer mydocument.js
import {
AUTH,
COMMON_SUCCESS,
COMMON_ERROR
} from './actions'
const initialState = {
dct: false
};
export default function reducer(state = initialState, action={}) {
switch (action.type) {
case AUTH:
return {
...state,
dct: action.data
};
case COMMON_ERROR:
return {
...state,
loginError: action.error
};
case COMMON_SUCCESS:
return {
...state
};
default:
return state;
}
}
// 类目列表
export function getLabelList(option) {
return {
types: [COMMON_SUCCESS, COMMON_ERROR],
promise: axios.get(`/service_mana/`, {params: option})
};
}
出口 index.js
/* eslint-disable import/no-named-as-default */
import {combineReducers} from 'redux';
import auth from './auth'; // 引入auth reduce
import mydocument from './mydocument';
// 合并多个reduce
export default combineReducers({
auth,
mydocument
});
在文件中的用法
import {connect} from 'react-redux';
import {bindActionCreators} from 'redux';
import * as authActions from '../../redux/reduces/auth';
import * as authAction from '../../redux/reduces/mydocument';```
@connect(
state => ({auth: state.auth, mydocument: state.mydocument}),
dispatch => ({
auths: bindActionCreators(authActions, dispatch),
documents: bindActionCreators(authAction, dispatch)
})
)
const {getInterUrlListByAdmin} = this.props.auths;
getInterUrlListByAdmin(option).then((res) => {
console.log(res);
});
const {updateUrl} = this.props.documents;
updateUrl().then((res) => {
if (res.res.data.flag === 0) {
message.info('修改成功');
} else {
message.info(res.res.data.msg);
}
});