使用react-redux而不是有效负载时,操作中的值是未定义的

时间:2017-12-11 00:37:22

标签: javascript reactjs react-redux

我正在使用React,React-redux,Redux,Express和Sequelize为学校项目构建应用程序。

在我的其他观点中,我收回数据,相应地插入和更新我的视图,但在这种情况下,我得到未定义

我刚刚从redux切换到react-redux,所以我提前道歉!

这是我的控制台的屏幕截图:

enter image description here

这是我的SingleCountry减速器,带有我的动作创建者和thunk ......

null

这是国家的缩减器:

'use strict';

import axios from 'axios';

// ACTION TYPES
const GET_COUNTRY = 'GET_COUNTRY';

// ACTION CREATORS
export function getCountry(oneCountry) {
  const action = {
    type: GET_COUNTRY,
    oneCountry,
  };
  return action;
}

//THUNK CREATORS

export function fetchCountry(countryId) {
  return function thunk(dispatch) {
    return axios
      .get('/api/countries/' + `${countryId}`)
      .then(res => res.data)
      .then(country => {
        const action = getCountry(country);
        dispatch(action);
      });
  };
}

// REDUCER
const reducer = function(state = [], action) {
  switch (action.type) {
    case GET_COUNTRY:
      return action.oneCountry;
    default:
      return state;
  }
};

export default reducer;

这是TopFiveCountries的缩减器:

'use strict';

import axios from 'axios';

// ACTION TYPES
const GET_COUNTRIES = 'GET_COUNTRIES';

// ACTION CREATORS
export function getCountries(countries) {
  const action = {
    type: GET_COUNTRIES,
    countries,
  };
  return action;
}

//THUNK CREATORS
export function fetchCountries() {
  return function thunk(dispatch) {
    return axios
      .get('/api/countries')
      .then(res => res.data)
      .then(countries => dispatch(getCountries(countries)))
      .catch(console.error);
  };
}

// REDUCER
const reducer = function(state = [], action) {
  switch (action.type) {
    case GET_COUNTRIES:
      return action.countries;
    // return { ...state, countries: action.countries };
    default:
      return state;
  }
};

export default reducer;

这是SingleCountry问题的观点:

'use strict';

import axios from 'axios';

// ACTION TYPES
const GET_TOP_COUNTRIES_BY_GFI = 'GET_TOP_COUNTRIES_BY_GFI';

// ACTION CREATORS
export function getTopFiveCountriesByGFI(topFiveCountries) {
  const action = {
    type: GET_TOP_COUNTRIES_BY_GFI,
    topFiveCountries,
  };
  return action;
}

//THUNK CREATORS
export function fetchTopFiveCountriesByGFI() {
  return function thunk(dispatch) {
    return axios
      .get('/api/countries/top-five-countries')
      .then(res => res.data)
      .then(countries => {
        const action = getTopFiveCountriesByGFI(countries);
        dispatch(action);
      })
      .catch(console.error);
  };
}

// REDUCER
const reducer = function(state = [], action) {
  switch (action.type) {
    case GET_TOP_COUNTRIES_BY_GFI:
      return action.topFiveCountries;
    // return { ...state, topFiveCountries: action.topFiveCountries };
    default:
      return state;
  }
};

export default reducer;

我的直觉告诉我它在我的axios请求中,或者可能是一些映射和调度的语法。

顺便说一下,我至少得到一个局部渲染(见下文)......并且没有错误。是否有任何预防措施可以做更好的诊断?

enter image description here

任何帮助将不胜感激!

更新 这就是我的import React, { Component } from 'react'; import { fetchCountry } from '../reducers/oneCountry'; import { connect } from 'react-redux'; import store from '../store'; class SingleCountry extends Component { constructor(props) { super(props); } render() { const flagStyle = { height: '50px', width: '100px', }; const oneCountry = this.props.oneCountry; return ( <div className="row"> <div className="twelve columns"> <h2> - Single Country -</h2> <table className="u-full-width"> <thead> <tr> <th>Name</th> <th>GFI</th> <th>Flag </th> </tr> </thead> <tbody> <tr> <td>{oneCountry.name}</td> <td>{oneCountry.GFI}</td> <td> <img style={flagStyle} src={oneCountry.flagUrl} /> </td> </tr> </tbody> </table> </div> </div> ); } } const mapDispatch = dispatch => ({ fetchCountry: countryId => { dispatch(fetchCountry(countryId)); }, }); const mapState = state => ({ oneCountry: state.oneCountry, }); export default connect(mapState, mapDispatch)(SingleCountry); 文件的样子......

store.js

更新

我最近的错误:

enter image description here

1 个答案:

答案 0 :(得分:1)

所以你没有派遣你的行动。您必须分派操作,并且由于它是异步的,因此最好显示某种UI以指示您从某种API获取。这是一个精简版:

class SingleCountry extends Component {
  constructor(props) {
    super(props)

    this.state = {
      loading: true
    }
  }

  componentDidMount() {
    const countryId = this.props.match.params.id;
    dispatch(this.props.fetchCountry(countryId))
      .then(() => {
        this.setState({
          loading: false
        });
      });
  }

  render() {
    const oneCountry = this.props.oneCountry;
    const loading = this.state.loading;
    return (
      <div className="row">
        <div className="twelve columns">
          <h2> - Single Country -</h2>

          {
            !loading ?
              <table className="u-full-width">
                <thead>
                  <tr>
                    <th>Name</th>
                    <th>GFI</th>
                    <th>Flag </th>
                  </tr>
                </thead>
                <tbody>
                  <tr>
                    <td>{oneCountry.name}</td>
                    <td>{oneCountry.GFI}</td>
                    <td>
                      <img style={flagStyle} src={oneCountry.flagUrl} />
                    </td>
                  </tr>
                </tbody>
              </table>
            :
              <img src={LOADING ICON HERE} />
          }
        </div>
      </div>
    );
  }
}

const mapState = (state, ownProps) => ({
  oneCountry: state.oneCountry,
  ...ownProps
});

这样做最初将loading的状态设置为true。然后,当组件安装时,您的请求将发送到API。同时,当请求正在运行时,将显示加载图标。请求完成后,将执行调度的then并将loading的状态设置为false。反过来,这将重新呈现您的组件并使用适当的数据显示表格。

另外,通知mapState现在使用第二个参数 - ownProps。问题是,当您connect组件到Redux商店时,您将丢失传递给组件的任何道具。在这种情况下,React Router将匹配的参数传递给SingleCountry,但由于connect而丢失了。这可以通过使用mapStatemapDispatch的第二个参数来解决。第二个参数包含允许传递给连接组件的props,您可以将它们传播到返回的对象中以在组件中访问它们。