Geoff_S
Geoff_S

Reputation: 5107

react native set state to text value from flatlist

In my react native app I have a sqlite storage solution and on this page I select a record from my table and use a flatlist to display the value, which works perfectly:

<FlatList
    data={this.state.FlatListActivityItems}
    ItemSeparatorComponent={this.ListViewItemSeparator}
    keyExtractor={(item, index) => index.toString()}
    renderItem={({ item }) => (
        <View key={item.id}>
            <Text style={styles.textUnderlines}>{item.activity} <Text style={styles.recordsTypes}>
                Minutes
            </Text></Text>
        </View>
    )}
/>

The problem is, I need to use the same value of item.activity in another place in this page as a data object which currently uses state

this.state = {activity: ''}

Is there a way on page load that I can just take the value {item.activity} and set it to the activity state so that I can use it anywhere else in the page with the state call?

Upvotes: 0

Views: 101

Answers (1)

abhikumar22
abhikumar22

Reputation: 1814

So where you are getting the data of flatlist, just there get the value and setstate it like below

vartemp = this.state.FlatListActivityItems[0].activity
this.setState({activity:vartemp})

I hope this helps....Thanks :)

Upvotes: 1

Related Questions