0
由於操作創建者的某些奇怪原因未能在我嘗試從組件觸發它時正確調用。我正在嘗試返回另一個函數來發送給我的reducer,就像redux-thunk的標準一樣。但是,我無法返回任何東西。從組件觸發時無法正確返回動作
組件被觸發「signinUser」行動功能,執行console.log顯示電子郵件和密碼值(操作文件的第7行),然後將其跳過動作的返回碼(第8行和以後)文件。
我在做一個愚蠢的錯誤嗎?我所有其他的組件都能正常工作我正在使用反應版本。 15.1
ACTION
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 **-----------------
COMPONENT
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);
我想通了。它在調用時不必調用第10行的調度函數,而必須使用redux-thunk。它之前調用過它,但我更新了我的組件,並更新了redux-form版本,我想這會影響到redux thunk如何調用返回的函數。在修復中,我只需解決操作文件中的承諾並返回一個包含狀態和數據的對象。 – andre
所以我基本上返回一個數據已經檢索到的對象,而不是返回一個方法,因此thunk在這種情況下應該調用(但沒有)出於某種原因。 – andre