How to get a var/const from render in React-Native

Berke

I want to use animation in my flatlist items. I create animation function, and a style which located in render. But I cant use that style, because my renderItem function is out of render, what I can do about it? I mean, how we can use a const which is located in render, in other function which is located outside of render?

state = {
        animation: new Animated.Value(1)
    };

startAnimation = () => {
        Animated.timing(this.state.animation, {
            toValue: 1.5,
            duration: 1500
        }).start();
    }

_renderItem = ({item}) => {

   return(
            <Animated.View style={[animatedStyles]} >
                <TouchableOpacity onLongPress={() => this.activateItem(item.id)} onPress={() => this.startAnimation}>
                    <Image source={ images[item.item_id].uri } style={{width: 60, height: 60}}/>
                </TouchableOpacity>
            </Animated.View>
        );

    };


    render(){

        const animatedStyles = {
            transform: [
                {
                    scale: this.state.animation
                }
            ]
        };

        return(

            <View style={{flex: 1}}>

                <FlatList
                      numColumns={4}
                      contentContainerStyle={{flexDirection: 'column', justifyContent: 'space-between', marginRight: 10, marginLeft: 10}}
                      data={this.state.items}
                      renderItem={this._renderItem}
                      keyExtractor={this._keyExtractor}
                />

            </View>

        );
Sam O'Brien

There's no reason why you can't access this.state in your _renderItem function so in this case it should be set here.

_renderItem = ({item}) => {
   return(
          <Animated.View style={{ transform: [{ scale: this.state.animation]}} >
            <TouchableOpacity onLongPress={() => this.activateItem(item.id)} onPress={() => this.startAnimation}>
            <Image source={ images[item.item_id].uri } style={{width: 60, height: 60}}/>
          </TouchableOpacity>
        </Animated.View>
    );

};

You'll also need to add this to your FlatList props. The extraData prop tells the rows to re-render whenever it changes.

extraData={this.state.animation}

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

how to render objects in react native?

From Dev

How to get react native version from JavaScript?

From Dev

Redux on React Native not able to render from reducer

From Dev

How to render component with ajax in react native?

From Dev

How to render checkbox on the <Image> (React Native)

From Dev

How to render items in two columns in react native?

From Dev

how to set state in render react native

From Dev

How to render components using function in React Native

From Dev

How to modify data structure fetched from elasticsearch to render item in a react native flatlist?

From Dev

How to get onPress event from ScrollView Component in React Native

From Dev

How to get data from the redux store and use it in component in React Native

From Dev

How to get a return value from async function to a variable in React Native?

From Dev

How to get image height and width from uri on React Native?

From Dev

How to get the Longitude and Latitude from a map in react-native?

From Dev

In React Native how to get key in flatlist dynamically from json array

From Dev

Get value from firebase and render it to render function in react

From Dev

Render HTML in React Native

From Dev

React Native staggered render

From Dev

React Native View Render

From Dev

React Native: Render on Fetch

From Dev

Can't get .map() working in React Native render()

From Dev

Render Content Dynamically from an array map function in React Native

From Dev

Render items from 2D Array in React Native JSX

From Dev

react native - getting state from storage on before render()

From Dev

can't render <MapView/> from react-native-maps

From Dev

How to render component from a function outside the render method in React?

From Dev

How to render random objects from an array in React?

From Dev

How to render a react component from a string name

From Dev

How to render a modal with map() from arraylist in react

Related Related

  1. 1

    how to render objects in react native?

  2. 2

    How to get react native version from JavaScript?

  3. 3

    Redux on React Native not able to render from reducer

  4. 4

    How to render component with ajax in react native?

  5. 5

    How to render checkbox on the <Image> (React Native)

  6. 6

    How to render items in two columns in react native?

  7. 7

    how to set state in render react native

  8. 8

    How to render components using function in React Native

  9. 9

    How to modify data structure fetched from elasticsearch to render item in a react native flatlist?

  10. 10

    How to get onPress event from ScrollView Component in React Native

  11. 11

    How to get data from the redux store and use it in component in React Native

  12. 12

    How to get a return value from async function to a variable in React Native?

  13. 13

    How to get image height and width from uri on React Native?

  14. 14

    How to get the Longitude and Latitude from a map in react-native?

  15. 15

    In React Native how to get key in flatlist dynamically from json array

  16. 16

    Get value from firebase and render it to render function in react

  17. 17

    Render HTML in React Native

  18. 18

    React Native staggered render

  19. 19

    React Native View Render

  20. 20

    React Native: Render on Fetch

  21. 21

    Can't get .map() working in React Native render()

  22. 22

    Render Content Dynamically from an array map function in React Native

  23. 23

    Render items from 2D Array in React Native JSX

  24. 24

    react native - getting state from storage on before render()

  25. 25

    can't render <MapView/> from react-native-maps

  26. 26

    How to render component from a function outside the render method in React?

  27. 27

    How to render random objects from an array in React?

  28. 28

    How to render a react component from a string name

  29. 29

    How to render a modal with map() from arraylist in react

HotTag

Archive