【问题标题】:Why is React saying “Invalid prop `children`” types is object not function?为什么 React 说“无效的 prop `children`”类型是对象而不是函数?
【发布时间】:2019-05-24 10:46:55
【问题描述】:

我对道具类型有疑问。出现警告消息,指出children 是对象而不是函数。

但是当我在对象中更改它时,我遇到了 npm Lint 的问题。

这说明不能是对象的类型。

如何避免这个问题?

组件代码:

import propTypes from 'prop-types';
import * as React from 'react';

export default function layout({ children, title }) {
  return (
    <div className="root">

      <h2>{title}</h2>

      {children}

    </div>
  );
}

layout.propTypes = {
  children: propTypes.func.isRequired,
  title: propTypes.string.isRequired
};

警告信息:

警告:道具类型失败:提供给layout 的类型为object 的无效道具children,应为function。 在布局中 在索引中 在容器中 应用内 在 Context.Provider 在 Context.Provider 在 Context.Provider 在 Context.Provider 中

【问题讨论】:

标签: javascript reactjs


【解决方案1】:

react children 的 props 不是一个函数,而是一个对象。

试试这个:孩子:PropTypes.element.isRequired

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-01-12
    • 1970-01-01
    • 2012-08-14
    • 1970-01-01
    • 2020-09-08
    • 1970-01-01
    • 2017-10-13
    相关资源
    最近更新 更多