M
M
Marat Ivanov2019-08-01 15:45:05
React
Marat Ivanov, 2019-08-01 15:45:05

How to correctly pass another component as a prop?

I get the error Objects are not valid as a React child (found: object with keys {$$typeof, type, compare}). If you meant to render a collection of children, use an array instead.

const Header = props => (
    <div className="navHeader">
      {props.top}
    </div>
  );

  
export const Top = React.memo(props => (
  <div className="top">
    <div className="site-identifier">
      <Logo />
      <Link to="/">
        Привет
      </Link>
    </div>
    {props.children}
  </div>
));

called in App
import Header, { Top } from "./views/header";
<Header top={Top}/>

The Top component must be passed in props. Thanks

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
Anton Spirin, 2019-08-01
@mrair

Consider the option:

const Header = ({ children }) => (
    <div className="navHeader">
      {children}
    </div>
  );

<Header>
  <Top />
</ Header>

You can also solve something like this:
const Header = ({ top }) => {
  const HeaderTop = top || Top;
  
  return (
    <div className="navHeader">
      <HeaderTop />
    </div>
  );
};

Then you will have a default component, and the top property can only be used where you need to use another component.
<Header top={OtherTopCompnent} /> // используем другой компонент

0
0xD34F, 2019-08-01
@0xD34F

const Header = ({ Top }) => (
  <div className="navHeader">
    <Top />
  </div>
);

...

<Header Top={Top} />

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question