Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/visual-studio-2008/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Reactjs React prop无法传递到功能组件中:";对象作为React子对象无效;_Reactjs - Fatal编程技术网

Reactjs React prop无法传递到功能组件中:";对象作为React子对象无效;

Reactjs React prop无法传递到功能组件中:";对象作为React子对象无效;,reactjs,Reactjs,我不知道为什么(我是新手),但我在尝试呈现简单组件“Intros.js”时遇到了一个简单的错误。以下是组件: Error occurred: Objects are not valid as a React child (found: object with keys {summary}). If you meant to render a collection of children, use an array instead. in div (created by Intros) in In

我不知道为什么(我是新手),但我在尝试呈现简单组件“Intros.js”时遇到了一个简单的错误。以下是组件:

Error occurred:
Objects are not valid as a React child (found: object with keys {summary}). If you meant to render a collection of children, use an array instead.
in div (created by Intros)
in Intros (created by App)
in Router (created by BrowserRouter)
in BrowserRouter (created by App)
in App
in StrictMode
简介:

import React from 'react';
import PropTypes from 'prop-types';

const Intros = (summary) => {
  return (
    <div className='intros'>
      ERROR HERE: {summary} variable can't be wrapped in curly braces; reason
      unknown
    </div>
  );
};

Intros.propTypes = {
  summary: PropTypes.string.isRequired,
};

Intros.defaultProps = {
  summary: 'Summary...',
};

export default Intros;
请注意,上面没有传递任何道具,但无论如何都应该有一个默认道具。下面是组件中使用
{summary}
时的错误:

Error occurred:
Objects are not valid as a React child (found: object with keys {summary}). If you meant to render a collection of children, use an array instead.
in div (created by Intros)
in Intros (created by App)
in Router (created by BrowserRouter)
in BrowserRouter (created by App)
in App
in StrictMode
为什么会发生此错误,如何更改Intros.js以将summary变量用作道具?使用
this.props.summary
也会失败

谢谢你的帮助。这是一个演示

您已经命名了变量
summary
,但这实际上是整个props对象,而不仅仅是summary prop。要么称之为道具,然后从中选择一个值:

const Intros = (props) => {
  const summary = props.summary;
或者使用对象分解以速记方式执行相同的操作:

const Intros = ({ summary }) => {
您已经命名了变量
summary
,但这实际上是整个props对象,而不仅仅是summary prop。要么称之为道具,然后从中选择一个值:

const Intros = (props) => {
  const summary = props.summary;
或者使用对象分解以速记方式执行相同的操作:

const Intros = ({ summary }) => {

您将
道具命名为
摘要

要解决您的问题,请使用下面的代码

const-Intros=(道具)=>{
返回(
此处错误:{props.summary}变量不能用大括号包装;原因是
未知的
);
};

您将
道具命名为
摘要
要解决您的问题,请使用下面的代码

const-Intros=(道具)=>{
返回(
此处错误:{props.summary}变量不能用大括号包装;原因是
未知的
);
};