Reactjs 从组件触发时未正确返回操作
出于某种奇怪的原因,当我试图从我的组件触发ByAction creator时,它没有被正确调用。我正试图返回另一个函数以分派给我的还原程序,这是redux thunk的标准。然而,我无法归还任何东西 组件正在触发'signinUser'操作功能,console.log显示电子邮件和密码值(操作文件第7行),然后跳过操作文件中的返回码(第8行及以后) 我犯了一个愚蠢的错误吗?我的所有其他组件都工作正常。我使用的是react版本。15.1 行动Reactjs 从组件触发时未正确返回操作,reactjs,redux,react-redux,redux-form,redux-thunk,Reactjs,Redux,React Redux,Redux Form,Redux Thunk,出于某种奇怪的原因,当我试图从我的组件触发ByAction creator时,它没有被正确调用。我正试图返回另一个函数以分派给我的还原程序,这是redux thunk的标准。然而,我无法归还任何东西 组件正在触发'signinUser'操作功能,console.log显示电子邮件和密码值(操作文件第7行),然后跳过操作文件中的返回码(第8行及以后) 我犯了一个愚蠢的错误吗?我的所有其他组件都工作正常。我使用的是react版本。15.1 行动 import axios from 'axios';
import axios from 'axios';
import {browserHistory} from 'react-router';
import {AUTH_USER, UNAUTH_USER, AUTH_ERROR, ROOT_AUTH_URL} from '../constants/auth.constants';
export function signinUser({email, password}) {
console.log('email and password in action', email, password);
---- ^^^ THIS CONSOLE PART WORKS.
**---- THE RETURN FUNCTION below is not working... BELOW CODE IS NOT RENDERING ANYTHING.**-------
return dispatch => {
console.log('pass the return'); ------> does not work :-(
// Submit email/password to the server
axios.post(`${ROOT_AUTH_URL}signin`, {email, password})
.then(response => {
console.log('response', response);
// - Update state to indicate user is authenticated: flag will turn to "true"
dispatch({type: AUTH_USER});
// - Save the JWT token in local storage
localStorage.setItem('token', response.data.token);
// - redirect to the route '/feature'
browserHistory.push('/');
})
.catch(() => {
// if request is bad...
// - Show an error to the user
dispatch(authError('Bad Login Info!'));
});
}
}
**---- ABOVE RETURN STATEMENT DOES NOT WORK **-----------------
组件
import React, {Component, PropTypes} from 'react';
import {reduxForm, Field} from 'redux-form';
import {signinUser} from '../../actions/auth.actions';
class Signin extends Component {
// used to take supplied inputs and check auth
handleFormSubmit({email, password}) {
// Need something to log user in
console.log('test', email, password);
signinUser({email, password});
**^^^^^ THIS PART is TRIGERRING THE ACTION**
}
renderAlert() {
if (this.props.errorMessage) {
return (
<div className="alert alert-danger">
<strong>Oops!</strong> {this.props.errorMessage}
</div>
);
}
}
render() {
// handleSubmit is a built in redux-form helper to bind ui to values
const {handleSubmit} = this.props;
return (
<form onSubmit={handleSubmit(this.handleFormSubmit.bind(this))}>
<fieldset className="form-group">
<label>Email:</label>
<Field name="email" component="input" type="text" required className="form-control"/>
</fieldset>
<fieldset className="form-group">
<label>Password:</label>
<Field name="password" component="input" type="password" required className="form-control"/>
</fieldset>
{this.renderAlert()}
<button action="submit" className="btn btn-primary">Sign in</button>
</form>
);
}
}
Signin.propTypes = {
signinUser: PropTypes.func,
errorMessage: PropTypes.string,
handleSubmit: PropTypes.func
};
function mapStateToProps(state) {
return {errorMessage: state.auth.error};
}
export default reduxForm({
form: 'signin'
}, mapStateToProps, signinUser)(Signin);
import React,{Component,PropTypes}来自'React';
从'redux form'导入{reduxForm,Field};
从“../../actions/auth.actions”导入{signinUser};
类签名扩展组件{
//用于获取提供的输入并检查身份验证
handleFormSubmit({电子邮件,密码}){
//需要一些东西来登录用户
console.log(“测试”、电子邮件、密码);
签名用户({电子邮件,密码});
**^^^^^这一部分正在触发动作**
}
renderAlert(){
如果(此.props.errorMessage){
返回(
哎呀!{this.props.errorMessage}
);
}
}
render(){
//handleSubmit是一个内置的redux表单帮助程序,用于将ui绑定到值
const{handleSubmit}=this.props;
返回(
电邮:
密码:
{this.renderAlert()}
登录
);
}
}
签名类型={
签名用户:PropTypes.func,
errorMessage:PropTypes.string,
handleSubmit:PropTypes.func
};
函数MapStateTops(状态){
返回{errorMessage:state.auth.error};
}
导出默认reduxForm({
表格:'签名'
},mapstatetops,Signin)(Signin);
一般来说,当我有这样的断开连接时,我说的“一般”是指200%的时间,这是因为拼写错误或丢失了某些内容。我注意到在你的行动中:
UTH_USER, ROOT_AUTH_URL
正在使用,但是
UNAUTH_USER, AUTH_ERROR
不是
在未经授权或出现错误的情况下,我不确定这是否会起到任何作用。检查浏览器devtools的“网络”选项卡以查找错误,然后在其他地方检查console.log并检查拼写
我希望你能找到它 我想出来了。这与redux thunk在调用第10行时没有调用调度函数有关。它以前调用过它,但我用更新的redux表单版本更新了我的组件,我猜它不知怎么地影响了redux thunk调用返回函数的方式。在修复中,我只是解析了动作文件中的承诺,并返回了一个包含状态和数据的对象。因此,我基本上返回了一个包含已检索数据的对象,而不是返回thunk由于某种原因应该调用(但没有)的方法。