在组件React中显示数据

时间:2019-03-21 14:11:08

标签: reactjs post redux components

我试图在POST请求中显示对象中的数据,但是当我尝试在组件中显示数据时,出现此错误:

“ TypeError:patito未定义”

我跟踪了所有数据和数组,所以我不知道自己收到了什么错误消息。

ConfigStore.js

import { ReduceStore } from 'flux/utils';
import ActionTypes from '../constants/AppConstants';
import AppDispatcher from '../dispatcher/AppDispatcher';
import config from '../../config';


class ConfigStore extends ReduceStore {

    getInitialState() {
        return {
            language: config.SiteConfig.defaultLanguage,
            languageLabels: {},
            races: [],
            tracks:{}
        };
    }

    reduce(state, action) {

        switch (action.type) {
            case ActionTypes.GET_TRACK:
                var newState = Object.assign({}, state);
                newState.tracks = action.data;
                return newState;
            case ActionTypes.GET_RACE:
                var newState = Object.assign({}, state);
                newState.races = action.data;
                return newState;
            default:
                return state;
        }
    }
}

export default new ConfigStore(AppDispatcher);

trackUtils.js

import AppDispatcher from '../dispatcher/AppDispatcher';
import ActionTypes from '../constants/AppConstants';
import ConfigStore from '../stores/ConfigStore';
import config from '../../config';
import axios from 'axios';

class trackUtil {

    constructor() {
        this.serverConfig = config.ServiceConfig;
        this.baseURL = this.serverConfig.url + ':' + this.serverConfig.port + '/';
        this.appConfig = config.AppConfig;
    }

    trackRequest(data) {
        const url = this.baseURL + 'BusRace/GetActiveTracksDate';

        const requestJSON = {
            DGSReq: {
                InParams: {
                    LogonSessionID: 17629,
                    ProfileID: 1,
                    RaceDate: "03/20/2019",
                    UIType: 1
                }
            }
        };

        axios.post(url, requestJSON)
            .then((response ) =>  {
                AppDispatcher.dispatch({
                    type: ActionTypes.GET_TRACK,
                    data: {...response.data.DGSResp}
                });
                console.log(response.data.DGSResp);
            })
            .catch((error) => {
                console.log(error);
            });
    };

}

export default new trackUtil();

trackjs (组件)

import React, { Component } from 'react';
import { Container } from 'flux/utils';
import ConfigStore from '../stores/ConfigStore';
import ActionTypes from '../constants/AppConstants';
import ActionCreators from "../actions/ActionCreators";
import { Redirect } from 'react-router-dom';

class Body extends Component {

    static getStores() {
        return [ConfigStore];
    };

    static calculateState() {
        let configData = ConfigStore.getState();
        return {
            configInfo: configData
        };
    };

    componentDidMount() {
        const params = {...this.state, ...{actionType: ActionTypes.GET_TRACK}};
        ActionCreators.actionTrigger(params);
    }

    render() {

        const patito= this.state.configInfo.tracks.TrackList;
        console.log(patito);
        let i = 0;
        for(i; i<patito.List.length; i++) {
            return(
                <div className="post card" key={patito.track-name}>

                </div>
            );
        }

        }
}


export default Container.create(Body);

希望有人可以帮助我,我希望有2天的时间能够解决此错误。

1 个答案:

答案 0 :(得分:0)

您在哪里定义了组件状态?我发现组件中没有状态线索。另外,您尝试通过this.state访问的 configStore 对象将无法访问,除非您没有通过setState更新状态或初始化状态。另外,我建议使用react的生命周期挂钩componentWillReceiveProps来更新组件的状态。 这可能会对您有所帮助。

class Body extends Component {
    constructor() {
      this.state={
        configInfo: {}
      }
    }
    static getStores() {
        return [ConfigStore];
    };

    static calculateState() {
        let configData = ConfigStore.getState();
        this.setState({configInfo: configData});
        return {
            configInfo: configData
        };
    };

    componentDidMount() {
        const params = {...this.state, ...{actionType: ActionTypes.GET_TRACK}};
        ActionCreators.actionTrigger(params);
    }

    render() {

        const patito= this.state.configInfo.tracks.TrackList;
        console.log(patito);
        let i = 0;
        for(i; i<patito.List.length; i++) {
            return(
                <div className="post card" key={patito.track-name}>

                </div>
            );
        }

        }
}