Redux动作调度不起作用

时间:2017-11-30 02:09:52

标签: javascript reactjs redux redux-thunk

我正在尝试在用户提交表单时发送操作。假设我有一个提交按钮,可以在表单上触发onSubmit事件(最终会在处理表单请求时显示一个微调器图标,但此时,提交按钮只显示truefalse表示是否显示微调器的值。

LoginPage.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;

LoginPageContainer.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);

index.js in actions /

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...
    }
};

index.js in reducers /

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;

奇怪的是,dispatch() loginUser()内的dispatch()工作后我的API调用有效,但lane :my_lane do other_lane paramter1_name:"1" parameter2:"2" end 不起作用。

我在网上看过很多教程,我的代码与我见过的内容相符,但由于某些原因,我无法弄清楚为什么调度动作不起作用。任何帮助将不胜感激。

1 个答案:

答案 0 :(得分:2)

问题在于:

const mapDispatchToProps = dispatch => {
    return {
        loginUser: (email, password) => { 
            dispatch(loginUser(email, password)); [DISPATCH ACTION]
        }
    }
};

所以loginUser是一个接收电子邮件,密码和电话调度的函数(loginUser(电子邮件,密码))

但loginUser(电子邮件,密码)不会返回操作,而是会返回一个函数。

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...
      }
};

因此,[DISPATCH ACTION]行不会调度操作,而是调度函数。

为了使其正确,您可以更改如下:

const mapDispatchToProps = dispatch => {
    return {
        loginUser: (email, password) => {
             loginUser(email, password)(dispatch);
        }
    }
};

因此,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...
    }
};

在mapDispatchToProps中:

const mapDispatchToProps = dispatch => {
    return {
        loginUser: loginUser(dispatch)
    }
};