Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

177
Visualizações
React list and keys, missing object properties

In this react tutorial, the author uses a snippet of code, starting on line 61:

const List = (props) => (
  <ul>
    {props.list.map((item) => (
      <Item key={item.objectID} item={item} />
    ))}
  </ul>
);

This function is called from within the const, App, <List list={stories} /> on line 35. And lastly, stories is an object:

const stories = [
    {
      title: 'React',
      url: 'https://reactjs.org/',
      author: 'Jordan Walke',
      num_comments: 3,
      points: 4,
      objectID: 0,
    },
    {
      title: 'Redux',
      url: 'https://redux.js.org/',
      author: 'Dan Abramov, Andrew Clark',
      num_comments: 2,
      points: 5,
      objectID: 1,
    },
  ];

I'm confused regarding line 61, specifically the code props.list.map. From the object, stories, there is no list attribute. However, in line 35, <List list={stories} /> seems to imply that stories will be referenced by the key, list.

My questions are:

  1. What's going on here?
  2. Why would this extra complexity be beneficial (globally speaking beyond the context of this code)?
about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

The prop is named list, but the value of that prop is being filled by the stories const. The reason you might do that is to make your List component generic and flexible.

If you wanted to reuse List for some other purpose, but had to pass it a prop named stories when you were actually passing it classes or some other type of list, it would be confusing.

In your case, just understand that props.list is actually stories here.

about 4 years ago · Juan Pablo Isaza Relatório

0

Given any <Component prop={value} />, you access value with prop:

function Component(props) {
    props.prop; // get value :O
}

Many people use destructuring because it is easier to read:

function Component({ prop }) {
    prop; // get value too :O
}

And also if you have an object of props for a component, you can spread them:

const theProps = { prop: 42 };

return <Component {...theProps} /> // same as <Component prop={theProps.prop} />

Furthermore, you don't have to destructure every prop, you can collect the rest into an object to be used normally:

function Component({ importantProp, ...notAsImportant }) {
    importantProp; // easy access and to use

    notAsImportant.prop; // you can still get the other props normally
}
about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda