Ol'*_*ble 4 javascript reactjs redux redux-thunk
我试图在用户提交表单时分派一个动作。假设我有一个提交按钮,它会触发onSubmit表单上的事件(最终会在处理表单请求时显示一个微调图标,但此时,提交按钮只显示一个true或false值,表示微调是否正在显示)。
登录页面.js
import React from 'react';
import { Link } from 'react-router-dom';
export class LoginPage extends React.Component {
constructor(props) {
super(props);
this.handleLogin = this.handleLogin.bind(this);
}
handleLogin(e) {
e.preventDefault();
let email = document.getElementById('userEmail').value;
let password = document.getElementById('userPassword').value;
this.props.loginHandler(email, password);
}
render() {
return (
<form onSubmit={ this.handleLogin }>
<input type="submit" value={ this.props.requestingLogin } />
</form>
);
}
}
export default LoginPage;
Run Code Online (Sandbox Code Playgroud)
登录页面容器.js
import React from 'react';
import { connect } from 'react-redux';
import LoginPage from '../../components/login/LoginPage';
import { loginUser } from '../../actions';
export class LoginPageContainer extends React.Component {
constructor(props) {
super(props);
}
render() {
return (
<LoginPage requestingLogin={ this.props.requestingLogin } loginHandler={ this.props.loginUser } />
);
}
}
const mapStateToProps = state => {
const { loginUserReducer } = state;
return {
requestingLogin: loginUserReducer.isRequestingLogin,
};
};
const mapDispatchToProps = dispatch => {
return {
loginUser: (email, password) => {
dispatch(loginUser(email, password));
}
}
};
export default connect(mapStateToProps, mapDispatchToProps)(LoginPageContainer);
Run Code Online (Sandbox Code Playgroud)
动作中的 index.js/
export const REQUEST_LOGIN = 'REQUEST_LOGIN';
function requestLogin() {
return {
type: REQUEST_LOGIN,
};
};
export const loginUser = (email, password) => {
return dispatch => {
// this works because when the button is clicked,
// I can successfully console log in here.
dispatch(requestLogin); // THIS DISPATCH IS NOT WORKING
// API call here...
}
};
Run Code Online (Sandbox Code Playgroud)
减速器中的 index.js/
import { combineReducers } from 'redux';
import { REQUEST_LOGIN } from '../actions';
const initialLoginState = {
isRequestingLogin: false,
};
function loginUserReducer(state = initialLoginState, action) {
switch(action.type) {
case REQUEST_LOGIN:
return Object.assign({}, state, { isRequestingLogin: true });
default:
console.log('returned from default');
return state;
}
}
const allReducers = combineReducers({
loginUserReducer,
});
export default allReducers;
Run Code Online (Sandbox Code Playgroud)
奇怪的是,我在dispatch()内部loginUser()工作后的 API 调用有效,但dispatch()无效。
我在网上看过很多教程,我的代码与我所看到的相匹配,但由于某种原因,我无法弄清楚为什么 dispatch action 不起作用。任何帮助将不胜感激。
问题在这里:
const mapDispatchToProps = dispatch => {
return {
loginUser: (email, password) => {
dispatch(loginUser(email, password)); [DISPATCH ACTION]
}
}
};
Run Code Online (Sandbox Code Playgroud)
所以 loginUser 是一个接收电子邮件、密码和调用 dispatch(loginUser(email, password)) 的函数
但是 loginUser(email, password) 不返回操作,而是返回一个函数。
export const loginUser = (email, password) => {
return dispatch => {
// this works because when the button is clicked,
// I can successfully console log in here.
dispatch(requestLogin); // THIS DISPATCH IS NOT WORKING
// API call here...
}
};
Run Code Online (Sandbox Code Playgroud)
所以该行[DISPATCH ACTION]不会调度一个动作,而是调度一个函数。
为了使其正确,您可以更改如下:
const mapDispatchToProps = dispatch => {
return {
loginUser: (email, password) => {
loginUser(email, password)(dispatch);
}
}
};
Run Code Online (Sandbox Code Playgroud)
所以, loginUser 返回一个函数,你用 dispatch 作为参数调用它。但是好像不好看。如何正确地做到这一点?
export const loginUser = dispatch => {
return (email, password) => {
// this works because when the button is clicked,
// I can successfully console log in here.
dispatch(requestLogin()); // THIS DISPATCH IS NOT WORKING
// API call here...
}
};
Run Code Online (Sandbox Code Playgroud)
在 mapDispatchToProps 中:
const mapDispatchToProps = dispatch => {
return {
loginUser: loginUser(dispatch)
}
};
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
14310 次 |
| 最近记录: |