失败的道具类型错误但加载了项目

问题描述 投票:2回答:3

我正在学习reactjs + redux-thunk并实现了以下修改过的代码(从其他站点复制原始版本):

 //reducers/items.js
 export function itemsHasErrored(state = false, action) {
    switch (action.type) {
        case 'ITEMS_HAS_ERRORED':
            return action.hasErrored;
        default:
            return state;
    }
}
export function itemsIsLoading(state = false, action) {
    switch (action.type) {
        case 'ITEMS_IS_LOADING':
            return action.isLoading;
        default:
            return state;
    }
}
export function items(state = {}, action) {
    switch (action.type) {
        case 'ITEMS_FETCH_DATA_SUCCESS':
            return action.items;
        default:
            return state;
    }
}

//App.js
import React, { Component } from 'react';

import ItemList from './components/ItemList';


class App extends Component {
  render() {
    return (
      <div className="App">
        <header className="App-header">
          <h1 className="App-title">Welcome to React</h1>
        </header>
        <ItemList/>
      </div>
    );
  }
}

export default App;


//ItemList.js
import React, { Component } from 'react';
import PropTypes from 'prop-types';
import { connect } from 'react-redux';
import { itemsFetchData } from '../actions/items';

class ItemList extends Component {
  componentDidMount() {
      //this.props.fetchData('http://599167402df2f40011e4929a.mockapi.io/items');
      this.props.fetchData('https://reqres.in/api/users');
  }

  render() {
      if (this.props.hasErrored) {
          return <p>Sorry! There was an error loading the items</p>;
      }

      if (this.props.isLoading) {
          return <p>Loading…</p>;
      }

      const items = this.props.items.data || [];
      console.log(items);

      return (
          <ul>
            {items.map((user, index) => (
                <li key={user.id}>
                    {user.first_name}
                </li>
            ))}
          </ul>
      );
  }
}

ItemList.propTypes = {
  fetchData: PropTypes.func.isRequired,
  items: PropTypes.object.isRequired,
  hasErrored: PropTypes.bool.isRequired,
  isLoading: PropTypes.bool.isRequired
};

const mapStateToProps = (state) => {
  return {
      items: state.items,
      hasErrored: state.itemsHasErrored,
      isLoading: state.itemsIsLoading
  };
};

const mapDispatchToProps = (dispatch) => {
  return {
      fetchData: (url) => dispatch(itemsFetchData(url))
  };
};

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

当我看着我的控制台时,我看到了以下内容:

Array(0)
Array(0)
ItemList.js:22 
index.js:2178 Warning: Failed prop type: Invalid prop `items` of type `object` supplied to `ItemList`, expected `array`.
    in ItemList (created by Connect(ItemList))
    in Connect(ItemList) (at App.js:13)
    in div (at App.js:9)
    in App (at index.js:14)
    in Provider (at index.js:13)
__stack_frame_overlay_proxy_console__ @ index.js:2178
ItemList.js:22 
Array(3)

项目加载,但很明显我做错了。我的动作创作者:

export const itemsHasErrored = (bool) => ({type: 'ITEMS_HAS_ERRORED', hasErrored: bool})
export const itemsIsLoading = (bool) => ({type: 'ITEMS_IS_LOADING',isLoading: bool})
export const itemsFetchDataSuccess = (items) => ({type: 'ITEMS_FETCH_DATA_SUCCESS', items})


export function itemsFetchData(url) {
    return (dispatch) => {
        dispatch(itemsIsLoading(true));

        fetch(url)
            .then((response) => {
                if (!response.ok) {
                    throw Error(response.statusText);
                }

                dispatch(itemsIsLoading(false));

                return response;
            })
            .then((response) => response.json())
            .then((items) => dispatch(itemsFetchDataSuccess(items)))
            .catch(() => dispatch(itemsHasErrored(true)));
    };
}
reactjs redux redux-thunk
3个回答
0
投票

更新了项目prop的PropTypes

//Original
items: PropTypes.object.isRequired,
//New
items: PropTypes.object.isRequired,

将项目减速器的初始状态更改为对象


0
投票

检查服务器或网络开发工具是什么项目,因为这是组件接收的项目所在的位置。


-1
投票

我在你的动作创建者中看到一个未定义的键:

export const itemsFetchDataSuccess = (items) => ({type: 'ITEMS_FETCH_DATA_SUCCESS', items})

或许解决方案是:

export const itemsFetchDataSuccess = (items) => ({type: 'ITEMS_FETCH_DATA_SUCCESS', items : items})
© www.soinside.com 2019 - 2024. All rights reserved.