简体   繁体   English

失败的道具类型:提供给`Body`的无效道具'app`,期望一个ReactElement

[英]Failed prop type: Invalid prop `app` supplied to `Body`, expected a single ReactElement

I'm trying to pass my App component to my Body component. 我正在尝试将我的App组件传递给我的Body组件。 It works and it seems to render fine, but I'm getting the above warning in my console. 它工作,似乎渲染得很好,但我在我的控制台中收到了上述警告。 Here is my code: 这是我的代码:

Document.js Document.js

import React from 'react';
import Head from './Head';
import Body from './Body';
import App from '../timer/App';

const Document = () => (
  <html>
    <Head />
    <Body app={App} />
  </html>
);

export default Document;

Body.js Body.js

import React, { PropTypes } from 'react';

const Body = ({ app: App }) => (
  <body>
    <App />
    <script src='/js/bundle.js'></script>
  </body>
);

Body.propTypes = {
  app: PropTypes.element.isRequired
};

export default Body;

App.js App.js

import React from 'react';

const App = () => (
  <h1>Hello</h1>
);

export default App;

There are no other errors, and the App is rendering fine, so I'm not sure why the propType is failing. 没有其他错误, App正常渲染,所以我不确定为什么propType失败。 Any help would be appreciated! 任何帮助,将不胜感激!

<Body app={App} />

App is not a React Element yet, its a function( PropTypes.func ) which is used to create the App Element. App不是React Element,它是一个用于创建App Element的函数( PropTypes.func )。

This is what you are passing right now 这就是你现在正在经历的

<Body app={function App() {
  return React.createElement(
    'div',
    null,
    React.createElement(
      'h1',
      null,
      'Hello'
    )
  );
}} />

If you want to pass a React Element, you should use <App/> instead of App 如果要传递React元素,则应使用<App/>而不是App

<Body app={<App/>} />

which is like 这就像

<Body app={React.createElement(
        'div',
        null,
        React.createElement(
          'h1',
          null,
          'Hello'
        )
)} />

so you'll be able to render it like this 所以你将能够像这样呈现它

const Body = ({ app }) => {
   return <body>
       {app}
       <script src='/js/bundle.js'></script>
       </body>
}

jsfiddle 的jsfiddle

暂无
暂无

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

相关问题 警告:道具类型失败:提供给“提供者”的对象类型为“对象”的道具儿童无效,需要一个ReactElement - Warning: Failed prop type: Invalid prop `children` of type `object` supplied to `Provider`, expected a single ReactElement 反应错误'Failed propType:提供给`Provider`的无效道具`children`,期望一个ReactElement' - React error 'Failed propType: Invalid prop `children` supplied to `Provider`, expected a single ReactElement' 失败的道具类型:向“表”提供了“功能”类型的道具“ rowSelection”无效,预期是“对象” - Failed prop type: Invalid prop `rowSelection` of type `function` supplied to `Table`, expected `object` %s 类型失败:%s%s,道具,提供给“图像”的无效道具“来源”,应为 [数字] 类型之一 - Failed %s type: %s%s, prop, Invalid prop `source` supplied to `Image`, expected one of type [number] reactjs 失败的道具类型:提供给“ForwardRef(IconButton)”的“object”类型的无效道具“className”,应为“string” - reactjs Failed prop type: Invalid prop `className` of type `object` supplied to `ForwardRef(IconButton)`, expected `string` 道具类型失败:提供给“ForwardRef(TablePagination)”的“string”类型的道具“count”无效,应为“number” - Failed prop type: Invalid prop `count` of type `string` supplied to `ForwardRef(TablePagination)`, expected `number` 警告:道具类型失败:提供给“ForwardRef(FormControl)”的“string”类型的道具“error”无效,预期为“boolean” - Warning: Failed prop type: Invalid prop `error` of type `string` supplied to `ForwardRef(FormControl)`, expected `boolean` 道具类型失败:提供给“ForwardRef(DataGrid)”的“object”类型的无效道具“rows”,预期为“array” - Failed prop type: Invalid prop `rows` of type `object` supplied to `ForwardRef(DataGrid)`, expected `array` 道具类型失败:提供给“按钮”的对象类型无效的道具“标签”,应为“字符串” - Failed prop type: Invalid prop `label` of type `object` supplied to `Button`, expected `string` 道具类型失败:提供给Styled(Container)的类型为number的道具类型无效,预期对象 - Failed prop type: Invalid prop `style` of type `number` supplied to `Styled(Container)`, expected `object`
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM