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);
我试图将这两个道具作为对象传递给组件,我得到了无效的道具类型错误。
我需要这两个道具作为对象,我很确定它们是对象,为什么它需要它们才能起作用?
答案 0 :(得分:3)
问题在于你的propTypes定义:
Game.propTypes = {
game: PropTypes.shape.isRequired,
actions: PropTypes.shape.isRequired,
};
您应该做的每件事:PropTypes.shape({}).isRequired
或PropTypes.object.isRequired
只需执行shape
即可将形状函数作为期望传递。