Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

157
Views
Get body content from custom react component

(Sorry if it's a noob question, I just started using React for a week, and didn't find the answer to this question in internet).

I have React component with two properties which is rendering one of this property into text body of <p>

function Item({ value, text }) {
  return (
    <div>
      {/* not relevant code here */}
      <p>{text}</p>
    <div>
  );
}

And I'm using it with this code:

<Item text="Foo" value="foo"/>
<Item text="Bar" value="bar"/>
<Item text="Baz" value="baz"/>

But I want to use it like this:

<Item value="foo">Foo</Item>
<Item value="bar">Bar</Item>
<Item value="baz">Baz</Item>

So how can I access the body of React Item element and how to pass it to properties?

about 4 years ago · Juan Pablo Isaza
3 answers
Answer question

0

My simple explanation of what props.children does is that it is used to display whatever you include between the opening and closing tags when invoking a component.

change your function component to

function Item(props) {
      return (
        <div>
          {props.children}
        <div>
      );
    }

Reference : react's props children

about 4 years ago · Juan Pablo Isaza Report

0

@Kirill I hope this will help you 😇.

function Item({ value, children }) {
  return (
    <div>
      {/* not relevant code here */}
      {children}
    <div>
  );
}

Then you can call your Item like this:

<Item value="foo">Foo</Item>
<Item value="bar">Bar</Item>
<Item value="baz">Baz</Item>

also you can pass other element as children to your Item Component also:

<Item value="foo">
   <p>Foo</p>
</Item>
about 4 years ago · Juan Pablo Isaza Report

0

I guess this should work.

function Item({ value, children }) {
  return (
    <div>
      {/* not relevant code here */}
      <p>{children}</p>
    <div>
  );
}
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!