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

138
Visualizações
How to output only 5 elements in a FlatList from JSON? React-native

I have a JSON file where new news items are constantly stored and added, their number is constantly changing. I want to output only the last 3 elements from this JSON file to the FlatList. How to do it?

Here is my JSON receipt and my FlatList:

  const [isLoading, setLoading] = useState(true);
  const [data, setData] = useState([]);

  const getNewsMain = async () => {
    try {
      const response = await fetch(`https://cdn.ertil-gorod.ru/json/ertnews.json?nocache`);
      const json = await response.json();
      setData(json.ertnews);
    } catch (error) {
      console.error(error);
    } finally {
      setLoading(false);
    }
  }

  useEffect(() => {
      getNewsMain();
  }, []);

            <FlatList 

              data={data}
              initialNumToRender={5} 
              keyExtractor={({ id }, index) => id} 
              renderItem={ ( { item } ) => (

                <TouchableOpacity 

                  style={ styles.ert__news__item } 
                  onPress={ ( ) => navigation.navigate( 'FullNews', item ) }>  

                    <Image 
                      style={ styles.ert__image__news } 
                      source={ { uri: item.img } } 
                    />

                    <Text style={ styles.ert__title__news }>{ item.name }</Text>

                    <Text style={ styles.ert__anons__news }>{ item.anons }</Text>
                    
                </TouchableOpacity>

            )} />
about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

If data is an array you can use the Array.slice

 <FlatList 
   data={data.slice(-3)}
   initialNumToRender={5} 
   keyExtractor={({ id }, index) => id} 
     renderItem={ ( { item } ) => (
       <TouchableOpacity 
         style={ styles.ert__news__item } 
         onPress={ ( ) => navigation.navigate( 'FullNews', item ) }>  
           <Image 
             style={ styles.ert__image__news } 
             source={ { uri: item.img } } 
           />
           <Text style={ styles.ert__title__news }>{ item.name }</Text>
           <Text style={ styles.ert__anons__news }>{ item.anons }</Text>
       </TouchableOpacity>
    )}
  />
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