警告:失败的道具类型:游戏:道具类型`游戏`无效;它必须是一个函数,通常来自React.PropTypes

时间:2017-03-13 00:58:40

标签: reactjs react-native eslint

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);

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

我需要这两个道具作为对象,我很确定它们是对象,为什么它需要它们才能起作用?

1 个答案:

答案 0 :(得分:3)

问题在于你的propTypes定义:

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

您应该做的每件事:PropTypes.shape({}).isRequiredPropTypes.object.isRequired

只需执行shape即可将形状函数作为期望传递。