[英]Redux action not firing - no errors
I'm trying to call a simple action on click which fires a dispatch action.我试图在点击时调用一个简单的动作来触发调度动作。 I can't seem to get a result or even indiciation that it's firing.
我似乎无法得到结果,甚至没有迹象表明它正在发射。
I'm trying to dispatch on click in a component.我正在尝试在组件中单击时分派。 I've also tried putting a console.log in the action to see if it even gets fired but it doesn't.
我还尝试在操作中放置一个 console.log 以查看它是否甚至被触发,但它没有。 Redux dev tools also doesn't suggest it even gets fired on click.
Redux 开发工具也不建议它在点击时被触发。
onClick={() => {
setAQIType(name);
}}
Action:行动:
import { SET_AQITYPE } from "./types";
export const setAQIType = (AQIType) => dispatch => {
dispatch({
type: SET_AQITYPE,
payload: { AQIType }
});
};
Reducer:减速器:
import { SET_AQITYPE } from '../actions/types';
const initialState = {
aqiType: 'DEFAULT',
loading: false,
};
export default function(state = initialState, action){
const { type, payload } = action;
switch(type){
case SET_AQITYPE:
return [...state, payload];
default:
return state;
}
}
Types:类型:
export const SET_AQITYPE = 'SET_AQITYPE';
Three errors,三个错误,
object
and not a list.object
而不是列表。aqiType
keyaqiType
键 To fix:修理:
export const setAQIType = (AQIType) => dispatch => {
dispatch({
type: SET_AQITYPE,
payload: AQIType // (3) pass as string
});
};
// In reducer
case SET_AQITYPE:
return { // (1) object
...state,
aqiType: payload // (2) specify aqiType key
};
This assumes that you've checked the basic example with connect()
and mapDispatchToProps
.这假设您已经使用
connect()
和mapDispatchToProps
检查了基本示例。
Most likely you missed to connect the component with the redux store, which means there is no dispatch function passed to your action.您很可能错过了将组件与 redux 存储连接,这意味着没有调度 function 传递给您的操作。
https://react-redux.js.org/using-react-redux/connect-mapdispatch https://react-redux.js.org/using-react-redux/connect-mapdispatch
Cheers干杯
Try to return
inside action function as below:尝试
return
内部操作 function 如下:
import { SET_AQITYPE } from "./types";
export const setAQIType = (AQIType) => dispatch => {
return dispatch({
type: SET_AQITYPE,
payload: { AQIType }
});
};
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.