如何在React中解决“ sourceSelector不是函数”的问题?

时间:2019-12-11 18:48:56

标签: javascript reactjs jsx

我在一个React项目中工作,我试图调度一个动作来获取请求。但是我收到“ TypeError:sourceSelector不是函数”。

这是ViewDocument.js中的代码

import React from 'react';
import { withRouter } from 'react-router-dom';
import { connect } from 'react-redux';

import AccountFlowHeader from 'components/Header/AccountFlowHeader';
import { LoadingModal } from 'components/Modals';

import { policiesSelector } from '../Policies/Policies.redux';
import { logOutAction } from '../Login/Login.actions';

import BasePage from '../BasePage';
import { getPolicyStatus } from './PolicyViewDocument.actions';

class PolicyViewDocument extends BasePage {
  constructor(props) {
    super(props);
    this.state = {
      showLoaderForPolicyDocument: false,
    };
  }

componentDidMount() {
    super.componentDidMount();
    const { currentPolicy: { contractNum } } = this.props;
    this.setState({ showLoaderForPolicyDocument: true }, () => {
      this.props.getPolicyStatus(contractNum, () => {
        this.setState({ showLoaderForPolicyDocument: false });
      }, (code) => {
        if (code === 'C002') {
          console.log(code);
        } else {
          this.setState({
            showLoaderForPolicyDocument: false,
          });
        }
      });
    });
  }

const mapDispatchToProps = {
  getPolicyStatus,
};

export default withRouter(connect(policiesSelector, mapDispatchToProps, { logOutAction })(PolicyViewDocument));

这是来自Actions.js的代码

import axios from 'axios';
import urls from 'constants/urls';
import { getErrorCode } from 'utils/common';
import globals from 'constants/globals';

export const getPolicyStatus = (contractNum, scb, fcb) => {
  axios({
    url: urls.getPolicyStatus(contractNum),
    method: 'get',
    timeout: globals.timeout,
  })
    .then((response) => {
      if (response.status === 200 && !response.data.errors) {
        if (scb) scb(response.data);
      } else if (fcb) fcb(getErrorCode(response));
    })
    .catch((e) => { if (fcb) fcb(getErrorCode(e.response)); });
};

我确定这与我使用mapDispatchToProps的方式有关,但是我无法解决。

1 个答案:

答案 0 :(得分:0)

您为什么打super.componentDidMount?我猜您在父组件中使用了sourceSelector,因为在共享代码段中没有看到任何sourceSelector东西。

尝试从此处删除super.componentDidMount