javascript - 即使在调度操作并接收到有效负载后,也不会调用 Reducer
问题描述
我使用create-react-app
and创建了一个反应应用程序react-redux
。mapDispatchToProps
我使用单击按钮调度一个动作,它返回一个有效负载。但是当我尝试检索mapStateToProps
组件中使用的道具时,它会返回初始状态。
我究竟做错了什么?
我试图彻底调试,我意识到动作已被分派,并且有效负载将其发送给动作创建者。但是在调度操作后不会触发减速器。
这可能是我如何称呼它或我如何设置我的减速器。
index.js 文件:
import React from 'react';
import './css/index.css';
import App from './App/App';
import * as serviceWorker from './serviceWorker';
import { render } from 'react-dom';
import { Provider } from 'react-redux';
import { createStore, applyMiddleware } from 'redux';
import rootReducer from './reducers';
import thunk from 'redux-thunk';
const store = createStore(rootReducer, applyMiddleware(thunk));
render(
<Provider store={store}>
<App />
</Provider>,
document.getElementById('root')
)
减速器/减速器.js
减速器/减速器.js
import { GET_RESP } from '../modules/actions'
function getInitialState () {
return {
predictions: [],
score: null
}
}
function gesResp (state, payload) {
return {
...state,
predictions: payload.predictions,
score: payload.score
}
}
export function Reducer (state, action) {
if (!state) {
return getInitialState();
}
switch(action.type) {
case GET_RESP:
return gesResp(state, action.payload);
default:
return state;
}
}
export default Reducer;
减速器/index.js
import { combineReducers } from 'redux';
import Reducer from './Reducer'
const rootReducer = combineReducers({
Reducer
});
export default rootReducer;
动作.js
import axios from 'axios';
// actions:
export const GET_RESP = 'GET_RESP';
// action creators:
export function gesResp (payload) {
return {
type: GET_RESP,
payload: payload
}
}
export function fetchRecommendations (description, resp) {
let url = 'myendpointurl';
let requestPayload = {
description: description,
resp: resp
}
return (dispatch) => {
return axios.post(url, requestPayload)
.then(function(res) {
gesResp(res.data);
})
}
}
组件文件:(我只发布相关代码):
handleSubmit () {
this.props.fetchMyRecommendations(Desc,
this.state.htmlContent);
}
const mapStateToProps = state => {
return {
predictions: state.Reducer.predictions,
score: state.Reducer.score
};
}
const mapDispatchToProps = dispatch => {
return {
fetchMyRecommendations: (Desc, userScore) =>
dispatch(fetchRecommendations(Desc, userScore))
};
}
export default connect(mapStateToProps, mapDispatchToProps)
(HomePage);
理想情况下,我想要的是mapStateToProps
返回预测数组和响应分数。我可以看到它们在网络调用中被返回并显示了动作调用。提前感谢任何可以提供帮助的人!:)
解决方案
您需要调度getReccommendations
以实际触发异步操作的减速器。尝试以下操作:
export function fetchRecommendations (job_description, resume) {
let url = 'myendpointurl';
let requestPayload = {
job_description: job_description,
resume: resume
};
return (dispatch) => {
return axios.post(url, requestPayload)
.then(function(res) {
dispatch(getReccommendations(res.data));
});
}
}
希望这会有所帮助!
推荐阅读
- full-text-search - Couchbase Lite - FTS 和索引
- delphi - 如何在 Child 以编程方式更改时自动更新父节点的 CheckState?
- java - 混淆时如何避免proguard修改包结构
- python - 我收到 'jsonschema.exceptions.UndefinedTypeCheck: Type 'string' is unknown to this type checker' 错误
- docker - 什么是共享标签/docker 图像的简单标签
- flutter - Flutter:调试小部件重建的正确方法
- jquery - 是否有可能通过 Ajax 在下拉列表中添加自定义年份?
- node.js - Sinon/沙盒测试说该函数从未被调用
- javascript - 从类名中获取 div 值
- android - SlidingUpPanelLayout 可见性问题