Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

159
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar

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 Denunciar

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda