警告:失败的道具类型:游戏:prop类型“游戏”无效;它必须是一个函数,通常来自react.proptypes


import React, { PropTypes } from 'react';
import { connect } from 'react-redux';
import { bindActionCreators } from 'redux';
// import UI components
import GameList from '../components/game/GameList';
// import actions
import gameActions from '../actions/game';
const Game = (props) => {
  const { game, actions } = props;
  return (
    <GameList game={game} actions={actions} />
  );
};
Game.propTypes = {
  game: PropTypes.shape.isRequired,
  actions: PropTypes.shape.isRequired,
};
function mapStateToProps(state) {
  return {
    game: state.game,
  };
}
function mapDispatchToProps(dispatch) {
  return {
    actions: bindActionCreators(gameActions, dispatch),
  };
}
export default connect(mapStateToProps, mapDispatchToProps)(Game);

我试图将这两个道具作为对象传递给组件,而我得到了无效的道具类型错误。

我需要这两个道具是对象,而且我很确定它们是对象,为什么它需要它们是功能?

问题在于您的proptypes定义:

Game.propTypes = {
  game: PropTypes.shape.isRequired,
  actions: PropTypes.shape.isRequired,
};

对于每个您都应该这样做:PropTypes.shape({}).isRequiredPropTypes.object.isRequired

仅通过执行shape,它将形状函数作为期望。

最新更新