PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Sunday, July 24, 2022

[FIXED] How can I change the sequence of output values from JSON to FlatList?

 July 24, 2022     json, react-native     No comments   

Issue

I'm new to React Native and I don't understand how to implement changing the sequence of output values from JSON format to FlatList. Let 's say my order in JSON goes like this: [id=1, name="test1"] , [id=2, name="test2"] and in FlatList it outputs from 1 to 2, and I need to reverse the output from 2 to 1. How do I do this?

  searchNews = async () =>
  {
    const ertAPI = await fetch(`${e_glav.e_url}/api/news/read.php?nocache`)
    const APIValue = await ertAPI.json();
    const APIResults = APIValue.ertnews
    this.setState({
      data:APIResults,
      isFetching: false
    })
  }

            <FlatList 

              data={this.state.data}
              onRefresh={() => this.onRefresh()}
              refreshing={this.state.isFetching}
              initialNumToRender={4} 
              contentContainerStyle={{ alignSelf: 'stretch' }}
              keyExtractor={({ id }, index) => id} 
              renderItem={({item}) => (

Solution

If you are looking for a custom sequence, you have to send a sequence from the backend(API), basically, send sorted data from API or write a function to generate an array in your desired sequence.

If you just want to reverse, then do the following

this.setState({
  data:APIResults.reverse(), // <-- do this
  isFetching: false
})

Document to refer: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/reverse



Answered By - Raj Rohit Yadav
Answer Checked By - Willingham (PHPFixing Volunteer)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing