Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

react native dynamically update flatlist data

import React, { useState } from 'react';
import { Text, View, StyleSheet, Button } from 'react-native';
import { FlatList } from 'react-native-gesture-handler';

export default function HomeScreen() {

  var initialElements = [
    { id : "0", text : "Object 1"},
    { id : "1", text : "Object 2"},
  ]

  const [exampleState, setExampleState] = useState(initialElements)

  const addElement = () => {
    var newArray = [...initialElements , {id : "2", text: "Object 3"}];
    setExampleState(newArray);
  }

  return (
    <View style={styles.container}>
        <FlatList
            keyExtractor = {item => item.id}  
            data={exampleState}
            renderItem = {item => (<Text>{item.item.text}</Text>)} />
        <Button
          title="Add element"
          onPress={addElement} />
    </View>
  );
}

const styles = StyleSheet.create({
  container: {
    flex: 1,
    backgroundColor: '#fff',
    width: '100%',
    borderWidth: 1
  },
});
 Run code snippet
Comment

PREVIOUS NEXT
Code Example
Javascript :: js get datatable attr value on click 
Javascript :: scroll out js threshold 
Javascript :: delete item from array 
Javascript :: react particles react 
Javascript :: Icons library in react 
Javascript :: json to string 
Javascript :: nestjs typeorm 
Javascript :: add a child html object to another html object in js 
Javascript :: disable zoom in app 
Javascript :: this.setstate is not a function in react native 
Javascript :: create multiple images in js 
Javascript :: node-schedule job on specific datetime 
Javascript :: react js docker 
Javascript :: adding logo to vscode extension development 
Javascript :: js how to see console day tomorrow 
Javascript :: nestjs swagger 
Javascript :: javascript add item to list 
Javascript :: add parameter at the end of url from jquery with refreshing 
Javascript :: how to find last element of an array 
Javascript :: react recursive component 
Javascript :: js remove escape characters from json 
Javascript :: javascript unique array 
Javascript :: get channel object using its name discod.js 
Javascript :: confluent kafka nodejs 
Javascript :: delete node from linked list 
Javascript :: extract string from text file javascript 
Javascript :: how to upload image in react js 
Javascript :: sort numbers in array in js 
Javascript :: javascript loops 
Javascript :: how to cast in javascript 
ADD CONTENT
Topic
Content
Source link
Name
8+8 =