Reactjs React Redux Saga函数从未执行

Reactjs React Redux Saga函数从未执行,reactjs,redux,redux-saga,Reactjs,Redux,Redux Saga,这是密码 //REDUCER.js import { call, take, fork } from 'redux-saga/effects'; import { request } from '../../utils'; export const LOGIN_REQUEST = "LOGIN_REQUEST"; const LOGIN_SUCCESS = "LOGIN_REQUEST"; const LOGIN_FAILED = "LOGIN_FAILED"; const initial

这是密码

//REDUCER.js

import { call, take, fork } from 'redux-saga/effects';
import { request } from '../../utils';

export const LOGIN_REQUEST = "LOGIN_REQUEST";
const LOGIN_SUCCESS = "LOGIN_REQUEST";
const LOGIN_FAILED = "LOGIN_FAILED";

const initialState = { authenticated: false, loading: false };

export default function (state = initialState, action) {
    switch (action.type) {
        case LOGIN_REQUEST:
            return { ...state, loading: true };
        case LOGIN_SUCCESS:
            return { ...state, loading: false, authenticated: true, user: action.payload };
        case LOGIN_FAILED:
            return { ...state, loading: false, };
        default:
            return { ...state }
    }
}

export function loginRequest(loginData) {
    return { type: LOGIN_REQUEST, loginData }
}

export function loginApi(formData) {
    return new Promise((resolve, reject) => {
        request.post('/login', formData)
            .then(response => {
                resolve(response)
            })
            .catch(error => reject(error));
    })
}

export function* handleLogin(formData) {
    try {
        console.log('handleLogin');
        const payload = yield call(loginApi, formData);

        console.log(payload)

    } catch (e) {
        console.log('error ', e);
    }
}

export function* watchLoginRequest() {
    yield take(LOGIN_REQUEST, handleLogin);
}

//ROOTSAGA.js
import { all } from 'redux-saga/effects';
import { watchLoginRequest } from './modules/auth/reducer';

export default function* Root() {
    yield all([
        watchLoginRequest,
    ])
}

//store.js
import createHistory from 'history/createBrowserHistory';
import { routerMiddleware } from 'react-router-redux';
import createSagaMiddleware from 'redux-saga';
import { applyMiddleware, compose, createStore } from 'redux';
import thunk from 'redux-thunk';
import reducer from '../rootReducer';
import rootSaga from '../rootSagas';
const history = createHistory();

const sagaMiddleware = createSagaMiddleware();

const middlewares = [
    sagaMiddleware,
    thunk,
    routerMiddleware(history),
];

/* eslint-disable no-underscore-dangle */
const composeEnhancers = process.env.NODE_ENV !== 'production' && window.__REDUX_DEVTOOLS_EXTENSION_COMPOSE__
    ? window.__REDUX_DEVTOOLS_EXTENSION_COMPOSE__
    : compose;
const store = createStore(reducer, composeEnhancers(applyMiddleware(...middlewares)));
/* eslint-enable */

sagaMiddleware.run(rootSaga);

export default store;
以下是我的组件:

import { Button, Form, Icon, Input, Row } from 'antd';
import React, { Component } from 'react';
import { connect } from 'react-redux';
import { config } from '../../../utils';
import { loginRequest } from '../reducer';
import { Div, DivLogo } from '../styles';

const FormItem = Form.Item;
const formDecorator = Form.create();

const reduxConnect = connect(null, { loginRequest });

class Login extends Component {
    static propTypes = {};

    static defaultProps = {};

    handleSubmit = (e) => {
        e.preventDefault();
        this.props.form.validateFields((err, values) => {
            if (!err) {
                console.log(values);
                this.props.loginRequest(values);
            }
        });
    };

    render() {
        const { getFieldDecorator } = this.props.form;

        return (
            <Div>
                <DivLogo>
                    <span>{config.logoText}</span>
                </DivLogo>
                <Form onSubmit={this.handleSubmit}>
                    <FormItem hasFeedback>
                        {getFieldDecorator('username', {
                            rules: [{ required: true, message: 'Please input your username' }],
                        })(
                            <Input
                                size="large"
                                prefix={<Icon type="user" style={{ fontSize: 13 }} />}
                                placeholder="Username" />,
                        )}
                    </FormItem>
                    <FormItem hasFeedback>
                        {getFieldDecorator('password', {
                            rules: [{ required: true, message: 'Please input your Password' }],
                        })(
                            <Input
                                type="password"
                                size="large"
                                prefix={<Icon type="lock" style={{ fontSize: 13 }} />}
                                placeholder="Password"
                            />,
                        )}
                    </FormItem>
                    <Row>
                        <Button type='primary' htmlType="submit" size='large' loading={false}>
                            Login
                        </Button>
                    </Row>
                </Form>
            </Div>
        );
    }
}

export default reduxConnect(formDecorator(Login));
从“antd”导入{按钮、表单、图标、输入、行};
从“React”导入React,{Component};
从'react redux'导入{connect};
从“../../../utils”导入{config};
从“../reducer”导入{loginRequest};
从“../styles”导入{Div,DivLogo};
const FormItem=表单项;
const formDecorator=Form.create();
const reduxConnect=connect(null,{loginRequest});
类登录扩展组件{
静态propTypes={};
静态defaultProps={};
handleSubmit=(e)=>{
e、 预防默认值();
this.props.form.validateFields((错误,值)=>{
如果(!err){
console.log(值);
this.props.loginRequest(值);
}
});
};
render(){
const{getFieldDecorator}=this.props.form;
返回(
{config.logoText}
{getFieldDecorator('用户名'{
规则:[{必需:true,消息:'请输入您的用户名'}],
})(
,
)}
{getFieldDecorator('密码'{
规则:[{必需:true,消息:'请输入您的密码'}],
})(
,
)}
登录
);
}
}
导出默认reduxConnect(formDecorator(登录));
当我提交表单时,它将执行loginRequest函数。我已检查它是否成功执行

问题是
watchLoginRequest
从未运行。它永远不会进入
handleLogin
功能。console.log从未在console上显示


任何解决方案?

在您的示例中,没有“实时进程”电路,源代码可以简化。因此,您的代码段只捕获特定类型的事件并调用回调。Effect
takeEvery
是此代码段的默认用例

export function handleLogin() {
    // Your definitive saga
}
// .....
export default function* Root() {
    yield [takeEvery(LOGIN_REQUEST, handleLogin)]
}
除此之外,不要混合使用佐贺处理器和减速机。如果您想从saga调用一些reducer,只需将操作名称分离为
请求
/
响应
格式,然后捕获所有
*\u请求
事件,并在Process Manager(saga)工作人员结束后放置
*\u响应