警告:道具类型失败:游戏:道具类型“游戏”无效;它必须是一个函数,通常来自 React.PropTypes
Warning: Failed prop type: Game: prop type `game` is invalid; it must be a function, usually from 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);
我试图将这两个道具作为对象传递给一个组件,但我收到无效的道具类型错误。
我需要这两个 props 是对象,我很确定它们是对象,为什么需要它们是函数?
问题在于您的 propTypes 定义:
Game.propTypes = {
game: PropTypes.shape.isRequired,
actions: PropTypes.shape.isRequired,
};
对于每个你应该做的事情:PropTypes.shape({}).isRequired
或 PropTypes.object.isRequired
通过 shape
它传递形状函数作为期望。
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);
我试图将这两个道具作为对象传递给一个组件,但我收到无效的道具类型错误。
我需要这两个 props 是对象,我很确定它们是对象,为什么需要它们是函数?
问题在于您的 propTypes 定义:
Game.propTypes = {
game: PropTypes.shape.isRequired,
actions: PropTypes.shape.isRequired,
};
对于每个你应该做的事情:PropTypes.shape({}).isRequired
或 PropTypes.object.isRequired
通过 shape
它传递形状函数作为期望。