React Redux TypeError:this.props 不是函数

IT技术 reactjs react-redux react-router jhipster react-props
2021-05-12 14:16:08

我一直在用这个拉我的头发,非常感谢一些帮助。

我收到以下错误:

类型错误:this.props.getAgencies 不是函数

这是从组件 (componentDidMount) 中的 this.props.getAgencies() 调用生成的。

代码如下所示

资本项目搜索.tsx

import React from 'react';
import {connect} from 'react-redux';
import {RouteComponentProps} from 'react-router-dom';
import {getAgencies} from './capital-project-search.reducer';

export interface ICapitalProjectSearchProps extends DispatchProps, RouteComponentProps<{ url: string }> {}
export type ICapitalProjectSearchState = {
}

export class CapitalProjectSearch extends React.Component<ICapitalProjectSearchProps, ICapitalProjectSearchState> {
  state: ICapitalProjectSearchState = {
  };
  constructor(props) {
    super(props);
  }
  componentDidMount() {
    this.props.getAgencies();
  };
  componentWillUnmount(): void {
  }
  render() {
    return (
      <div>
        <p>Test</p>
      </div>
    );
  }
}

const mapDispatchToProps = {
  getAgencies
};

type DispatchProps = typeof mapDispatchToProps;

export default connect(
  null,
  mapDispatchToProps
)(CapitalProjectSearch);

资本项目search.reducer.ts

import axios from 'axios';
import {FAILURE, REQUEST, SUCCESS} from 'app/shared/reducers/action-type.util';
import {DropDownType} from "app/entities/primecpmsa/capital-project/capital-project-common";

export const ACTION_TYPES = {
  FETCH_AGENCIES: 'capitalProjectSearch/FETCH_ACENCIES',
};

const initialState = {
  errorMessage: null,
  agencies: [] as Array<DropDownType>
};

export type CapitalProjectSearchState = Readonly<typeof initialState>;

// Reducer
export default (state: CapitalProjectSearchState = initialState, action): CapitalProjectSearchState => {
  switch (action.type) {
    case REQUEST(ACTION_TYPES.FETCH_AGENCIES):
      return {
        ...state,
        errorMessage: null
      };
    case FAILURE(ACTION_TYPES.FETCH_AGENCIES):
      return {
        ...state,
        errorMessage: action.payload
      };
    case SUCCESS(ACTION_TYPES.FETCH_AGENCIES):
      return {
        ...state,
        agencies: action.payload.data
      };
    default:
      return state;
  }
};

const gEntityApiUrl = 'services/cpmsa/api/g-entities';

// Actions
export const getAgencies = () => {
  return ({
    type: ACTION_TYPES.FETCH_AGENCIES,
    payload: axios.get(gEntityApiUrl)
  });
};

索引.tsx

import React from 'react';
import {Switch} from 'react-router-dom';

import ErrorBoundaryRoute from 'app/shared/error/error-boundary-route';
import CapitalProjectDeleteDialog from './capital-project-delete-dialog';
import {CapitalProjectSearch} from "app/entities/primecpmsa/capital-project/capital-project-search";

const Routes = ({ match }) => (
  <>
    <Switch>
      <ErrorBoundaryRoute exact path={`${match.url}/search`} component={CapitalProjectSearch} />
    </Switch>
    <ErrorBoundaryRoute path={`${match.url}/:key/delete`} component={CapitalProjectDeleteDialog} />
  </>
);
export default Routes;

3个回答

你的代码:

const mapDispatchToProps = {
  getAgencies
};

应该像

const mapDispatchToProps =()=> {
  return {getAgencies:getAgencies}
};

这可能是因为函数的 ES6 语法错误

const mapDispatchToProps = {
  getAgencies
};

将返回对象包裹在括号中

const mapDispatchToProps = () => ({
  getAgencies
});

所以我改变了

import {CapitalProjectSearch} from "app/entities/primecpmsa/capital-project/capital-project-search";

import CapitalProjectSearch from "app/entities/primecpmsa/capital-project/capital-project-search";

它奏效了。