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

208
Vistas
React/Javascript passing props spread syntax vs. specific syntax

I am coding a recursive component.

If I pass props like this: <RecursiveComponent itemProps={data} />, it won't work. I have to pass like this: <RecursiveComponent {...data} />. Why is that?

<script src="https://cdnjs.cloudflare.com/ajax/libs/react/16.6.3/umd/react.production.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react-dom/16.6.3/umd/react-dom.production.min.js"></script>
import * as React from 'react'

const data = {
  name: 'Level 1',
  children: [
    {
      name: 'Level 2',
      children: [
        {
          name: 'Level 3'
        }
      ]
    },
    {
      name: 'Level 2.2',
      children: [
        {
          name: 'Level 3.2'
        }
      ]
    }
  ]
}

const RecursiveComponent = (itemsProp) => {
  const hasData = itemsProp.children && itemsProp.children.length;

  return (
    <>
      {itemsProp.name}
      {hasData && itemsProp.children.map((child) => {
        console.log(child);
        return <RecursiveComponent {...child} key={child.name} />
      })}
    </>
  )
}

function App() {
  return (
    <div className="App">
      <RecursiveComponent {...data} />
    </div>
  );
}

export default App;

about 4 years ago · Juan Pablo Isaza
2 Respuestas
Responde la pregunta

0

React components take an object as first argument: the props. When you use jsx, the attributes you pass are converted to an object.

<Component name="test" id={42} ok={true} /> // props = { name: "test", id: 42, ok: true }

Then your component receive the props like this:

const Component = (props) => {
    console.log(props.name) // => "test"
    // the rest of your component...
}

// Most people will use destructuration to use directly the attribute name
const Component = ({ name, id, ok }) => {
    console.log(name) // => "test"
    // the rest of your component...
}

The spread operator allow you to fill attributes for every properties of an object

const data = { name: "test", id: 42, ok: true }
<Component {...data} />
//  is the same as :
<Component name={data.name} id={data.id} ok={data.ok} />

In your case when you pass itemsProp={data} you actually have the first argument of your component like this

const RecursiveComponent = (itemsProp) => {
    // here itemsProp = { itemsProp : { name : "Level 1", children : [...] }}
    // so you have to use it this way
    const hasData = itemsProp.itemsProp.children && itemsProp.itemsProp.children.length;
}
about 4 years ago · Juan Pablo Isaza Denunciar

0

you can use it with passing props <RecursiveComponent itemProps={data} /> also, see below , notice the {} brace on props while getting props in function component.

Different way 1:-

    const RecursiveComponent = (props) => {
           const hasData = props.itemProps.children && props.itemProps.children.length
}

you can use like above as well.

Solution 2:-

import React from "react";

const data = {
  name: "Level 1",
  children: [
    {
      name: "Level 2",
      children: [
        {
          name: "Level 3"
        }
      ]
    },
    {
      name: "Level 2.2",
      children: [
        {
          name: "Level 3.2"
        }
      ]
    }
  ]
};

const RecursiveComponent = ({itemProps}) => {
  console.log("itemsprops", itemProps);
  const hasData = itemProps.children && itemProps.children.length;

  return (
    <>
      {itemProps.name}
      {hasData && itemProps.children.map((child) => {
        console.log(child);
        return <RecursiveComponent itemProps={child} key={child.name} />
      })}
    </>
  );
};

function App() {
  return (
    <div className="App">
      <RecursiveComponent itemProps={data} />
    </div>
  );
}

export default App;
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