簡體   English   中英

我收到 TypeError: this.props.function is not a function while from an api

[英]I am getting TypeError: this.props.function is not a function while fetching from an api

我正在嘗試從 api 獲取數據,但出現錯誤: TypeError: this.props.getPeople is not a function,雖然通過代碼一切看起來都不錯,如下所示:

人員組件.js

import React, { Component } from 'react';
import './people-component-styles.css';
import { Container, Row, Col, Card } from 'react-bootstrap';
import { connect } from 'react-redux';
import { getPeople } from '../../actions/index'
import 'react-lazy-load-image-component/src/effects/blur.css';
import 'animate.css/animate.min.css';

export class People extends Component {

    componentDidMount() {
        // console.log(this.props);
        this.props.getPeople();
    }

    render() {
        // console.log(this.props);
        return (
            <Row className='main'>
                hello!  
            </Row>
        );
    }
}

const mapStateToProps = state => ({
    people: state.people
})

const mapDispatchToProps = dispatch => ({
    getPeople: () => dispatch(getPeople())
})

export default connect(
    mapStateToProps,
    mapDispatchToProps
)(People);

動作/ index.js

export const getPeople = () => {
    console.log("yes");
    return async dispatch => {
        const response = await fetch('https://dma.com.eg/api.php?action=getPeople', {
            method: 'GET'
        })
        const json = await response.json();
        dispatch({ type: "GET_PEOPLE", payload: json });
    }
}

減速器/ index.js

const INITIAL_STATE = {
    people: []
}

const rootReducer = (state = INITIAL_STATE, action) => {

    switch (action.type) {
        case "GET_PEOPLE":
            return ({
                ...state,
                people: state.people.concat(action.payload)
            })
        default:
            return state;
    }
};

export default rootReducer

感謝@Brian Thompson ,我將People組件作為名稱導入,而它作為默認導出。它已修復。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM