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

303
Visualizações
Iterating over children in stateless components react/react-native

I was working on a react-native application and I created a common component for display list items.

<View style={styles.container}>
  <ItemsWithSeparator style={styles.itemsContainer}>
     <AppRow />
     <AppRow />
  </ItemsWithSeparator>
</View>

Now, my ItemListSeparator is just iterates over the children and renders the list, so I thought I would make this a stateless component.

const ItemsWithSeparator = function ({children,style}) {
  const childrenList = [];
  const length = React.Children.count(children);
  React.Children.forEach(
    children,
    (child,ii) => {
      childrenList.push(child);
      if (ii !== length -1) {
        childrenList.push(
          <View
            key={`separator-${ii}`}
            style={[styles.separator]}
          />
        );
      }
    }
  );
  return (
    <View style={style}>
      {children}
    </View>
  );
};

But this throws an error saying 'React' not found.

However, it works fine with class based components. Following is the code which is working fine.

class ItemsWithSeparator extends React.Component {

  render () {
    const {children,style} = this.props;
    const childrenList = [];
    const length = React.Children.count(children);
    React.Children.forEach(
      children,
      (child,ii) => {
        childrenList.push(child);
        if (ii !== length -1) {
          childrenList.push(
            <View
              key={`separator-${ii}`}
              style={[styles.separator]}
            />
          );
        }
      }
    );
    return (
      <View style={style}>
        {children}
      </View>
    );
  }
}

Can anyone help me understanding this? TIA!!

Update:

I was just trying few something and apparently got his to work:-

const ItemsWithSeparator = function ({children,style,...props}) {
  const childrenList = [];
  const length = React.Children.count(children);
  React.Children.forEach(
    children,
    (child,ii) => {
      childrenList.push(child);
      if (ii !== length -1) {
        childrenList.push(
          <View
            key={`separator-${ii}`}
            style={[styles.separator]}
            {...props}
          />
        );
      }
    }
  );
  return (
    <View style={style}>
      {children}
    </View>
  );
};

But I am still a bit confused on how is this working. If someone could explain I would really be great.

over 4 years ago · Santiago Trujillo
1 Respostas
Responde à pergunta

0

Here is refactored version so you don't have to do this weird React.Children stuff :D Notice that you can return array when mapping children. There you can make if statements if needed.

const ItemsWithSeparator = ({children, style, ...props}) => {
  const finalFields = children.map((child, index) => {
    return [
      child,
      index !== children.length - 1 && (
        <View key={index} {...props} style={styles.separator} />
      )
    ];
  });

  return (
    <View style={style}>
      {finalFields}
    </View>
  );
};
over 4 years ago · Santiago Trujillo 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