Reducer 不触发,尽管 action 触发
Reducer does not fire although action does
我完全是 redux 的菜鸟,这是我的问题:
我想从我的后端获取一系列技能,它成功地获取了我想要的数据,但是当动作被调度时,reducer 没有给出任何让步。
这是我的 NewSkill.js:
import React, {Component} from 'react';
import { connect } from 'react-redux';
import Select from 'react-select';
import Aux from '../../../hoc/Aux';
import * as actions from '../../../store/actions/index';
import { withRouter } from 'react-router-dom';
class NewSkill extends Component {
componentDidMount () {
this.props.onInitSkills();
}
render () {
return (
<Aux>
<p>Please make sure your tag has not yet been created : </p>
{/* <Select options={this.state.skills.skills} /> */}
</Aux>
);
};
}
const mapStateToProps = state => {
return {
skills: state.skills.skills,
error: state.skills.error
}
}
const mapDispatchToProps = dispatch => {
return {
onInitSkills: () => dispatch(actions.initSkills())
}
}
export default withRouter(connect(mapStateToProps, mapDispatchToProps) (NewSkill));
这是减速器:
import * as actionTypes from '../actionTypes';
import { updateObject } from '../../utility';
const initialState = {
skills: null,
error: false
}
const setSkills = ( state, action ) => {
console.log("REDUCER");
return updateObject( state, {
skills: action.skills.skills,
error: false
});
}
const fetchSkillsFailed = ( state, action ) => {
return updateObject( state, { error: true } );
}
const reducer = ( state = initialState, action ) => {
switch ( action.type ) {
case actionTypes.SET_SKILLS: return setSkills( state, action );
case
actionTypes.FETCH_SKILLS_FAILED: return fetchSkillsFailed( state, action );
default: return state;
}
};
export default reducer;
操作如下:
import * as actionTypes from './actionTypes';
import axios from '../../axios';
export const initSkills = () => {
return dispatch => {
axios.get('/skills')
.then( response => {
dispatch(setSkills(response.data))
})
.catch( error => {
dispatch(fetchSkillsFailed());
});
};
};
export const setSkills = (skills) => {
console.log("ACTION");
return {
type: actionTypes.SET_SKILLS,
skills: skills
};
};
export const fetchSkillsFailed = () => {
return {
type: actionTypes.FETCH_SKILLS_FAILED
}
};
操作索引:
export {
auth,
logout,
authCheckState
} from './auth';
export {
initSkills
} from './skill';
动作类型:
export const FETCH_SKILLS_FAILED = 'FETCH_SKILLS';
export const SET_SKILLS = 'SET_SKILLS';
以及效用函数:
export const updateObject = (oldObject, updatedProperties) => {
return {
...oldObject,
...updatedProperties
};
};
所以基本上每次控制台只记录 ACTION 而不是 REDUCER。
已经尝试了几个小时,但无法判断出了什么问题。感谢您的帮助!
编辑:这是包裹整个东西的 index.js
import React from 'react';
import ReactDOM from 'react-dom';
import './index.css';
import App from './App';
import registerServiceWorker from './registerServiceWorker';
import { BrowserRouter } from 'react-router-dom';
import { Provider } from 'react-redux';
import { createStore, applyMiddleware, compose, combineReducers } from 'redux';
import thunk from 'redux-thunk';
import authReducer from './store/actions/reducers/auth';
const composeEnhancers = window.__REDUX_DEVTOOLS_EXTENSION_COMPOSE__ || compose;
const rootReducer = combineReducers({
auth: authReducer
});
const store = createStore(rootReducer, composeEnhancers(
applyMiddleware(thunk)
));
const app = (
<Provider store={store}>
<BrowserRouter>
<App />
</BrowserRouter>
</Provider>
);
ReactDOM.render( app, document.getElementById( 'root' ) );
registerServiceWorker();
我认为问题是你的状态的技能为空,将其替换为空数组,我尝试使用此代码笔并调用了 reducer(我删除了异步调用,因为代码笔让我头疼中间件)
const initialState = {
skills: [],
error: false
}
您没有在 combineReducers
中添加减速器。你那里只有 authReducer
。
我完全是 redux 的菜鸟,这是我的问题:
我想从我的后端获取一系列技能,它成功地获取了我想要的数据,但是当动作被调度时,reducer 没有给出任何让步。
这是我的 NewSkill.js:
import React, {Component} from 'react';
import { connect } from 'react-redux';
import Select from 'react-select';
import Aux from '../../../hoc/Aux';
import * as actions from '../../../store/actions/index';
import { withRouter } from 'react-router-dom';
class NewSkill extends Component {
componentDidMount () {
this.props.onInitSkills();
}
render () {
return (
<Aux>
<p>Please make sure your tag has not yet been created : </p>
{/* <Select options={this.state.skills.skills} /> */}
</Aux>
);
};
}
const mapStateToProps = state => {
return {
skills: state.skills.skills,
error: state.skills.error
}
}
const mapDispatchToProps = dispatch => {
return {
onInitSkills: () => dispatch(actions.initSkills())
}
}
export default withRouter(connect(mapStateToProps, mapDispatchToProps) (NewSkill));
这是减速器:
import * as actionTypes from '../actionTypes';
import { updateObject } from '../../utility';
const initialState = {
skills: null,
error: false
}
const setSkills = ( state, action ) => {
console.log("REDUCER");
return updateObject( state, {
skills: action.skills.skills,
error: false
});
}
const fetchSkillsFailed = ( state, action ) => {
return updateObject( state, { error: true } );
}
const reducer = ( state = initialState, action ) => {
switch ( action.type ) {
case actionTypes.SET_SKILLS: return setSkills( state, action );
case
actionTypes.FETCH_SKILLS_FAILED: return fetchSkillsFailed( state, action );
default: return state;
}
};
export default reducer;
操作如下:
import * as actionTypes from './actionTypes';
import axios from '../../axios';
export const initSkills = () => {
return dispatch => {
axios.get('/skills')
.then( response => {
dispatch(setSkills(response.data))
})
.catch( error => {
dispatch(fetchSkillsFailed());
});
};
};
export const setSkills = (skills) => {
console.log("ACTION");
return {
type: actionTypes.SET_SKILLS,
skills: skills
};
};
export const fetchSkillsFailed = () => {
return {
type: actionTypes.FETCH_SKILLS_FAILED
}
};
操作索引:
export {
auth,
logout,
authCheckState
} from './auth';
export {
initSkills
} from './skill';
动作类型:
export const FETCH_SKILLS_FAILED = 'FETCH_SKILLS';
export const SET_SKILLS = 'SET_SKILLS';
以及效用函数:
export const updateObject = (oldObject, updatedProperties) => {
return {
...oldObject,
...updatedProperties
};
};
所以基本上每次控制台只记录 ACTION 而不是 REDUCER。 已经尝试了几个小时,但无法判断出了什么问题。感谢您的帮助!
编辑:这是包裹整个东西的 index.js
import React from 'react';
import ReactDOM from 'react-dom';
import './index.css';
import App from './App';
import registerServiceWorker from './registerServiceWorker';
import { BrowserRouter } from 'react-router-dom';
import { Provider } from 'react-redux';
import { createStore, applyMiddleware, compose, combineReducers } from 'redux';
import thunk from 'redux-thunk';
import authReducer from './store/actions/reducers/auth';
const composeEnhancers = window.__REDUX_DEVTOOLS_EXTENSION_COMPOSE__ || compose;
const rootReducer = combineReducers({
auth: authReducer
});
const store = createStore(rootReducer, composeEnhancers(
applyMiddleware(thunk)
));
const app = (
<Provider store={store}>
<BrowserRouter>
<App />
</BrowserRouter>
</Provider>
);
ReactDOM.render( app, document.getElementById( 'root' ) );
registerServiceWorker();
我认为问题是你的状态的技能为空,将其替换为空数组,我尝试使用此代码笔并调用了 reducer(我删除了异步调用,因为代码笔让我头疼中间件)
const initialState = {
skills: [],
error: false
}
您没有在 combineReducers
中添加减速器。你那里只有 authReducer
。