简体   繁体   English

警告:失败的道具类型:游戏:道具类型`游戏`无效; 它必须是一个函数,通常来自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);

I am trying to pass these two props as objects to a component and I am getting the invalid prop type error. 我试图将这两个道具作为对象传递给组件,我得到无效的道具类型错误。

I need these two props to be objects, and I am pretty sure they are objects, why it needs them to be function? 我需要这两个道具作为对象,我很确定它们是对象,为什么它需要它们才能起作用?

The issue lies in your propTypes definition: 问题在于propTypes定义:

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

for each you should be doing: PropTypes.shape({}).isRequired or PropTypes.object.isRequired 对于你应该做的每一个: PropTypes.shape({}).isRequiredPropTypes.object.isRequired

by just doing shape it's passing the shape function as the expectation. 通过shape它将形状函数作为期望传递。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 失败的道具类型:聊天:道具类型`房间`无效; 它必须是一个函数,通常来自React.PropTypes - Failed prop type: Chat: prop type `room` is invalid; it must be a function, usually from React.PropTypes 它必须是一个函数,通常来自 React.PropTypes - it must be a function, usually from React.PropTypes 应用:道具类型“商店”无效; 它必须是一个函数,通常来自“ prop-types”包,但收到了“ undefined” - App: prop type `store` is invalid; it must be a function, usually from the `prop-types` package, but received `undefined` 反应警告:失败的道具类型:提供的“对象”类型的无效道具 - React Warning: Failed prop type: Invalid prop of type `Object` supplied React-(嵌套)道具类型无效; 它必须是一个函数 - React - (nested) prop type is invalid; it must be a function 反应警告:“失败的道具类型:检查器不是函数” - React Warning: "Failed prop type: checker is not a function" 警告:道具类型失败:提供给“路由”的道具“组件”无效:道具不是路由中的有效 React 组件 - Warning: Failed prop type: Invalid prop 'component' supplied to 'Route': the prop is not a valid React component in Route 警告:失败的道具类型:提供给“路由”的“对象”类型的无效道具“组件”,预期的“函数”使用 react-router-dom 错误 - Warning: Failed prop type: Invalid prop `component` of type `object` supplied to `Route`, expected `function` Error using react-router-dom 警告:propType失败:使用React的类型为`array` expected`object`的prop - Warning: Failed propType: Invalid prop of type `array` expected `object` with React 警告:propType失败:提供给“ LaptopHandling”的类型为“ function”的无效prop`swimmingSnapshot`, - Warning: Failed propType: Invalid prop `swimmingSnapshot` of type `function` supplied to `LaptopHandling`,
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM