简体   繁体   English

错误:React 正在返回对象作为 React 子项无效

[英]Error: React is Returning Objects are not valid as a React child

I'm trying to render some child elements using a reusable button component (class component).我正在尝试使用可重用按钮组件(类组件)呈现一些子元素。 The button component accepts some props, and it also renders a Link conditionally.按钮组件接受一些道具,它也有条件地呈现一个链接。

Below is the code:下面是代码:

  render() {
    const {
      className,
      link,
      children,
      style,
      onClick,
      id,
      value,
      tabIndex,
      name
    } = this.props;

    if (link) {
  
      return (
        <Link className={className} onClick={onClick} to={link}>
          {children} 
        </Link>
      );
    }
    return (
      <button
        value={value}
        style={style}
        className={''}
        onClick={onClick}
        id={id}
        tabIndex={tabIndex}
        name={name}
      >
        {children}
      </button>
      
    );
  }
}

export default Button; 

But React keeps returning an error saying:但是 React 不断返回一个错误说:

 ```Uncaught Error: Objects are not valid as a React child (found: object with keys {__typename, symbol}). If you meant to render a collection of children, use an array instead.```

Both the button and link components render any children they contain.按钮和链接组件都呈现它们包含的任何子项。

You are passing an object, React can't render that because it's a complex object, basically it doesn't know what to do with that object. You can pass array or string or integer or other primitive types .您正在传递一个 object,React 无法呈现它,因为它是一个复杂的 object,基本上它不知道如何处理该 object。您可以传递数组或字符串或 integer 或其他原始类型

I have made an example here .我在这里做了一个例子。

Uncomment the line that passes object to the Test component and you see a similar error.取消注释将object传递给Test组件的行,您会看到类似的错误。

you must use your component this way: (asumming is named class Button...)你必须这样使用你的组件:(asumming 被命名为 class Button ...)

<Button>Hello there!</Button>

Where "Hello there."在哪里“你好”。 is the children prop.是儿童道具。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM