Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
164 views
in Technique[技术] by (71.8m points)

javascript - SectionList/FlatList isn't showing the correctly list

I am creating a page that has a SectionList, however, it is not showing all items, there is always 1 item less than the selected Array.

Not only on this page, but on another one that has a FlatList the same thing happens, it does not render the last item SectionList at the end

This is the array that I put at Section List

Array [
  Object {
    "data": Array [
      Object {
        "goleiro": false,
        "nome": "1",
      },
      Object {
        "goleiro": false,
        "nome": "2",
      },
      Object {
        "goleiro": false,
        "nome": "4",
      },
      Object {
        "goleiro": false,
        "nome": "4",
      },
      Object {
        "goleiro": false,
        "nome": "54",
      },
      Object {
        "goleiro": false,
        "nome": "4",
      },
    ],
    "title": "Time 1",
  },
  Object {
    "data": Array [
      Object {
        "goleiro": false,
        "nome": "1",
      },
      Object {
        "goleiro": false,
        "nome": "3",
      },
      Object {
        "goleiro": false,
        "nome": "1",
      },
      Object {
        "goleiro": false,
        "nome": "2",
      },
      Object {
        "goleiro": false,
        "nome": "1",
      },
      Object {
        "goleiro": false,
        "nome": "23",
      },
    ],
    "title": "Time 2",
  },
  Object {
    "data": Array [
      Object {
        "goleiro": false,
        "nome": "34",
      },
      Object {
        "goleiro": false,
        "nome": "1",
      },
      Object {
        "goleiro": false,
        "nome": "5",
      },
      Object {
        "goleiro": false,
        "nome": "4",
      },
    ],
    "title": "Time 3",
  },
]

This is the code of page:

import React from 'react';
import { Container, Cabecalho, TitlePage, Nome, ListaNomes, TituloSecao, TimeNumber, ListaView } from './styles';
import { MaterialIcons } from '@expo/vector-icons';


const finalPage = ({route, navigation}) => {
  const {data, esporte, quantidadeTimes} = route.params;

  function separarTimes(arr, tamanho) {
    var novoArray = [];
    var i = 0;
    var numeroTime = 1;
    while (i < arr.length) {
      novoArray.push({title: "Time "+ numeroTime, data: arr.slice(i, i + tamanho)});
      numeroTime += 1;
      i += tamanho;
  
    }
    return novoArray;
  }

  const timesSeparados = separarTimes(data, Math.ceil(data.length/quantidadeTimes))
  console.log(timesSeparados);

  return (
      <Container>
          <Cabecalho>
              <TitlePage>{esporte}</TitlePage>
          </Cabecalho>
          <ListaView>
            <ListaNomes
              sections={timesSeparados}
              keyExtractor={(item, index) => item + index}
              renderItem={({item }) => {
                return (
                  <Nome>{item.nome}</Nome>
                );
              }}
              renderSectionHeader={({ section }) => (
                <TituloSecao>
                  <TimeNumber>{section.title}</TimeNumber>
                </TituloSecao>
              )}
            />
          </ListaView>
      </Container>
  );
}

export default finalPage;
question from:https://stackoverflow.com/questions/65873877/sectionlist-flatlist-isnt-showing-the-correctly-list

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...