当使用react,redux和thunk从API提取一些数据时,我遇到了错误  TypeError:this.props.getAnimals不是函数\由线触发this.props.getAnimals();使用Redux Tools,我们可以看到this.props.getAnimals函数已成功执行,再次显示了animals_are_loading,get_animals和animals_are_loading动作,并且状态正在正确更新,正如您期望调用了功能this.props.getAnimals()。知道发生了什么吗?container / Animals.jsimport React, { Component } from 'react';import { connect } from 'react-redux';import { getAnimals } from '../../actions';class Animals extends Component { componentDidMount() { this.props.getAnimals(); } renderAnimalsList() { return ... } renderLoading() { return ... } render() { return ( <div> <h1>Animals</h1> { (this.props.animalsAreLoading) ? this.renderLoading() : this.renderAnimalsList() } </div> ) }}function mapStateToProps(state) { return { animals: state.animals.animals, animalsAreLoading: state.animals.isLoading }}function mapDispatchToProps(dispatch) { return { getAnimals }}export default connect(mapStateToProps, getAnimals)(Animals)动作/ index.jsimport axios from 'axios';import { GET_ANIMALS_SUCCESS, ANIMALS_ARE_LOADING } from './types';export function getAnimals() { return function(dispatch) { dispatch(animalsAreLoading(true)) // ACTION SEEN IN REDUX TOOLS axios .get(`${ROOT_URL}/animals`, { headers: {authorization: localStorage.getItem('token')} }) .then(response => { console.log(response.data.animals) // THIS RETURNS DATA! // ACTION SEEN IN REDUX TOOLS dispatch(getAnimalsSuccess(response.data.animals)) // ACTION SEEN IN REDUX TOOLS dispatch(animalsAreLoading(false)) return response }) }}export function animalsAreLoading(bool) { return { type: ANIMALS_ARE_LOADING, payload: bool }}export function getAnimalsSuccess(animals) { return { type: GET_ANIMALS_SUCCESS, payload: animals }} 最佳答案 试试看:用您的代码替换这些代码行function mapDispatchToProps(dispatch) { return ({ getAnimals: () => dispatch(getAnimals()) })}错误:  TypeError:this.props.getAnimals不是一个函数很清楚!因为将return object函数的mapDispatchToProps()合并到React组件的props对象之后。 getAnimals属性实际上是not a function。function mapDispatchToProps(dispatch) { return { getAnimals // this property is properly not a function }}我所做的错误修复是将值设置为getAnimals属性:getAnimals: () => dispatch(getAnimals())使它的价值成为调度动作创建者的功能。关于javascript - React-Redux TypeError:this.props.getAnimals不是一个函数,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/52547018/
10-13 08:03