javascript - 为什么 React 说 “Invalid prop ` Children`” 类型是对象而不是函数?

标签 javascript reactjs

我对 Prop 类型有疑问。出现警告消息,指出 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
};

警告消息:

Warning: Failed prop type: Invalid prop children of type object supplied to layout, expected function. in layout in Index in Container in App in Context.Provider in Context.Provider in Context.Provider in Context.Provider

最佳答案

React Children 属性不是一个函数,而是一个对象。

试试这个: child :PropTypes.element.isRequired

关于javascript - 为什么 React 说 “Invalid prop ` Children`” 类型是对象而不是函数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56291122/

相关文章:

javascript - 将按钮链接到同一页面中的标题

javascript - 如何在用户滚动页面时触发图像移动?

javascript - 在不使用 .then 的情况下在类里面链式 promise

javascript - 以 HTML 格式打印 Python 输出

reactjs - 防止带有样式组件的 Component/PureComponent 在状态更改后重新渲染所有项目

javascript - 我怎样才能访问 react 中的 key ?

javascript - 防止默认并返回 false;如果我添加 if else 语句

javascript - 网站的 Facebook 登录 : Best practice to handle user Facebook logout?

javascript - 使用 Ethers.js 获取钱包地址中存储的所有 ENS 域的列表

reactjs - 如何区分React组件渲染的原因 : something from the inside or from the outside of the component?