How can I limit the elements in a FlatList in React Native?

0

I'm trying to limit the elements, in this case the posts, I just want to show 4 posts.

My Code:

componentDidMount() {
   return fetch(ConfigApp.URL+'json/data_posts.php')
     .then((response) => response.json())
     .then((responseJson) => {
       this.setState({
         isLoading: false,
         dataPosts: responseJson
       }
     })}

render() {
return (
    <FlatList
      data={ this.state.dataPosts }
      renderItem={({item}) => 
            <Image source={{uri: ConfigApp.IMAGESFOLDER+item.post_image}}/>              
keyExtractor={(item, index) => index}
/>
    
asked by anonfidusa 06.05.2018 в 00:16
source

1 answer

0

You can do it in the following way, it sure works for you:

componentDidMount() {
  return fetch(ConfigApp.URL+'json/data_posts.php')
    .then((response) => response.json())
    .then((responseJson) => {
      this.setState({
        isLoading: false,
        dataPosts: responseJson.slice(0,4) //<---- AQUI FILTRAMOS 4 PRIMEROS ELEMENTOS
      }
    })}

render() {
return (
   <FlatList
     data={ this.state.dataPosts }
     renderItem={({item}) => 
           <Image source={{uri: ConfigApp.IMAGESFOLDER+item.post_image}}/>              
     keyExtractor={(item, index) => index}
  />
    
answered by 07.05.2018 / 12:32
source