[英]React/Redux app actions not being dispatched properly
I'm trying to set up my first React app using Redux, but I'm currently having some issues with dispatching actions. 我正在尝试使用Redux设置我的第一个React应用,但是我目前在调度动作方面遇到一些问题。 I have the following files for my actions so far:
到目前为止,我的操作有以下文件:
constants/AuthActionTypes.js: 常量/ AuthActionTypes.js:
export const AUTH_PENDING = 'AUTH_PENDING';
export const AUTH_SUCCESS = 'AUTH_SUCCESS';
export const AUTH_FAIL = 'AUTH_FAIL';
actions/AuthActions.js: 动作/ AuthActions.js:
import * as AuthActionTypes from '../constants/AuthActionTypes';
function authPending() {
return { type: AuthActionTypes.AUTH_PENDING };
}
function authSuccess(response) {
return { type: AuthActionTypes.AUTH_SUCCESS };
}
export function login(username, password) {
return dispatch => {
dispatch(authPending());
// nothing really happens yet
dispatch(authSuccess());
};
}
I've also got a login component containing an HTML form, and a login
function that gets called when the form is submitted. 我还有一个包含HTML表单的登录组件,以及一个在提交表单时调用的
login
函数。
components/Login.js 组件/ Login.js
...
login (e) {
e.preventDefault();
var username = ReactDOM.findDOMNode(this.refs.username).value;
var password = ReactDOM.findDOMNode(this.refs.password).value;
this.props.dispatch(AuthActions.login(username, password));
}
...
The function is triggered, but... something isn't right. 该函数已触发,但是... 某些错误 。 I use
redux-devtools
, and it only dispatches a single action - surely, it should dispatch the actions from both authPending
and authSuccess
? 我使用
redux-devtools
,它只调度一个动作-当然,它应该从authPending
和authSuccess
调度动作? In addition, the devtools pane doesn't display any action type, and I get the following warning in the console: 此外,devtools窗格不显示任何操作类型,并且在控制台中收到以下警告:
Warning: Failed propType: Invalid prop
action
of typefunction
supplied toLogMonitorEntry
, expectedobject
.警告:propType失败:提供给
LogMonitorEntry
(预期object
的类型function
无效propaction
。 Check the render method ofLogMonitor
.检查
LogMonitor
的呈现方法。
What am I doing wrong here? 我在这里做错了什么? What have I missed?
我错过了什么? I've primarily looked at the examples at https://github.com/rackt/redux/tree/master/examples/real-world and https://github.com/yildizberkay/redux-example , and I can't see what I'm doing differently that makes my own app break.
我主要在https://github.com/rackt/redux/tree/master/examples/real-world和https://github.com/yildizberkay/redux-example中查看了示例,但我做不到看看我在做什么,这会使我自己的应用程序中断。
... and a couple of minutes later, I've stumbled across the answer: I didn't have the redux-thunk
middleware applied to my store. ...几分钟后,我迷迷糊糊地回答了这个问题:我没有在商店中应用过
redux-thunk
中间件。 Applied that, and everything works as expected. 应用这一点,一切都会按预期进行。
You can do w/o thunk. 你可以不做任何事。 change
更改
this.props.dispatch(AuthActions.login(username, password));
to 至
this.props.dispatch(AuthActions.authPending());
this.props.dispatch(AuthActions.authSuccess());
Ofcourse, you have to import those two methods/action-creators. 当然,您必须导入这两个方法/动作创建者。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.