最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - pass component as prop in reactjs - Stack Overflow

programmeradmin1浏览0评论

I thought it was simple but looks like I´m doing something wrong

const name = "Mike"
...
<MyComponent
    data={
        <Picture /> + " " + name
    }
/>

const Picture () => return <img src="..." />

const myComponent (props) => return props.data

i´m getting this output

[object Object] Mike

thank you!

I thought it was simple but looks like I´m doing something wrong

const name = "Mike"
...
<MyComponent
    data={
        <Picture /> + " " + name
    }
/>

const Picture () => return <img src="..." />

const myComponent (props) => return props.data

i´m getting this output

[object Object] Mike

thank you!

Share edited Apr 22, 2020 at 17:34 handsome asked Apr 22, 2020 at 17:26 handsomehandsome 2,43211 gold badges54 silver badges81 bronze badges 3
  • What is user.name used for? – wentjun Commented Apr 22, 2020 at 17:28
  • try this: return <div>{props.data}</div> – Zain Ul Abideen Commented Apr 22, 2020 at 17:32
  • Does this answer your question? Pass react ponent as props – Emile Bergeron Commented Apr 22, 2020 at 18:07
Add a ment  | 

4 Answers 4

Reset to default 5

You should separate the passing of both items into 2 different props, rather than joining them together (they are both different types - ReactNode, and string). The advantage of doing so is that there will be better type checking, especially if you are using TypeScript or PropTypes.

<MyComponent
  data={<Picture />}
  name={user.name}
/>

Then in the myComponent itself, you should do this if you are trying to print the name under the Picture.

const myComponent = ({ data, name }) => (
  <>
    {data}
    {name}
  </>
);
const Picture = () => <img alt="" src="" />;
const MyComponent = props => props.data;

export default function App() {
  return (
    const user = ...
    <MyComponent
      data={
        <>
          <Picture />
          {user.name}
        </>
      }
    />
  );
}

Any value should be passed thru {your value}

{user.name}

In this part of code, you shouldn't use return const MyComponent = props => props.data;

If you wanna return it in classic way write like this:

const MyComponent = props => {
    return props.data
};

You can check this example:

Parent Component

import React, {Component, useEffect, useState} from 'react';
import {ChildComp} from "./ChildComp";
import {Hello} from "./Hello";

export class ParentComp extends Component {

    render() {
        return (
            <div>
                <ChildComp>
                    <Hello/>
                </ChildComp>
            </div>
        );
    }
}

Child Component

import React, {Component, useEffect, useState} from 'react';

export class ChildComp extends Component {

    render() {
        return (
            <div>
                {this.props.children}
            </div>
        );
    }
}

Hello Component

import React, {Component, useEffect, useState} from 'react';

export class Hello extends Component {

    render() {
        return (
            <div>
                <h1>Hello World</h1>
            </div>
        );
    }
}

You can pass a ponent as a prop. However, look at this line:

<Picture /> + " " + user.name

Since <AnyComponent /> results in a React element, and a React element is internally represented as an object, you're essentially doing this:

{} + " " + user.name

which is why you're seeing [object Object] in your output. What you should do is

function MyComponent({ pictureElement, name }) {
  return (
    <div>
      {pictureElement} {name}
    </div>
  );
}
发布评论

评论列表(0)

  1. 暂无评论