how to call other functions in flat list render function?

Iman Salehi

I have a Flatlist which i call other functions inside that render function

   otherFunc(){
       alert('some thing')
   }
   item(data){
      return(
          //...something..
          {this.otherFunc()} <<<<<<<<<problem is here...its undefined
      );
   }
   render() {
       <FlatList
            ref={ref => this.scrollView = ref}
            data={this.state.foods}
            extraData={this.state}
            keyExtractor={this._keyExtractor}
            renderItem={this.Item}
            horizontal
            onEndReached={(x) => { this.loadMore() }}
            onEndReachedThreshold={0.5}
      />
   }

i return something in this.Item which they pretty render in Flatlist but i can't call other functions of my component inside this.item! and i even can't point this.props.navigation or any other this key word inside that. i get undefined object error.

Jordan Chevalier

When you use this.item in the FlatList component you need to bind this function to the class, you have 3 main ways to do that:

  • in your constructor :

    contructor(props) {
        this.item = this.item.bind(this);
        // when using this.item everywhere, this will refer to the class in the method
    }
    
  • if you are using the experimental public class fields syntax, you can use class fields to correctly bind callbacks :

    item = (data) => {
      //now this refer to the class
    }
    
  • Or directly in the component:

    <FlatList
        ref={ref => this.scrollView = ref}
        data={this.state.foods}
        extraData={this.state}
        keyExtractor={this._keyExtractor}
        renderItem={(data) => this.item(data)}
        horizontal
        onEndReached={(x) => { this.loadMore() }}
        onEndReachedThreshold={0.5}
    />
    

i prefer the second way

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Java

How to Call a Function inside a Render in React/Jsx

From Dev

How to call RequireJS functions in other files? How to separate RequireJS files?

From Dev

How to call functions in a main view model from other view models?

From Dev

Using eval function in Ruby to call other functions

From Dev

How to retrieve a function from a series of functions and call it

From Dev

How to call the functions of an other element contained inside template (ShadowDOM)?

From Dev

How to convert a dictionary into a flat list?

From Dev

How to test functions other than render in a React class using jest?

From Dev

How to call a function from a list?

From Dev

How to call a function in the render function?

From Dev

Can I call the same function inside of two other functions?

From Dev

Flat list does not show in other component

From Dev

Call other functions from a callback function

From Dev

How to call functions in a main view model from other view models?

From Dev

How to call a function of an object in a list?

From Dev

How i can call functions from other class? Swift, Xcode

From Dev

How to call two functions and use their results as arguments for each other?

From Dev

How to call the functions of an other element contained inside template (ShadowDOM)?

From Dev

How to call a function in the render function?

From Dev

javascript Function constructed with new Function cannot call other functions

From Dev

Programmatically define a new function from a list of other functions

From Dev

how to call a module in a function in pp where that fuction has other functions in it?

From Dev

Call variables in construct function from other functions in Codeigniter

From Dev

How to call asynchronous recursive functions after each other?

From Dev

How to call random function from list of functions?

From Dev

How to call WordPress function from other files

From Dev

Call function after other functions in Dart

From Dev

Render countdowns on each flat list item

From Dev

how to call function inside reactjs render method properly?

Related Related

  1. 1

    How to Call a Function inside a Render in React/Jsx

  2. 2

    How to call RequireJS functions in other files? How to separate RequireJS files?

  3. 3

    How to call functions in a main view model from other view models?

  4. 4

    Using eval function in Ruby to call other functions

  5. 5

    How to retrieve a function from a series of functions and call it

  6. 6

    How to call the functions of an other element contained inside template (ShadowDOM)?

  7. 7

    How to convert a dictionary into a flat list?

  8. 8

    How to test functions other than render in a React class using jest?

  9. 9

    How to call a function from a list?

  10. 10

    How to call a function in the render function?

  11. 11

    Can I call the same function inside of two other functions?

  12. 12

    Flat list does not show in other component

  13. 13

    Call other functions from a callback function

  14. 14

    How to call functions in a main view model from other view models?

  15. 15

    How to call a function of an object in a list?

  16. 16

    How i can call functions from other class? Swift, Xcode

  17. 17

    How to call two functions and use their results as arguments for each other?

  18. 18

    How to call the functions of an other element contained inside template (ShadowDOM)?

  19. 19

    How to call a function in the render function?

  20. 20

    javascript Function constructed with new Function cannot call other functions

  21. 21

    Programmatically define a new function from a list of other functions

  22. 22

    how to call a module in a function in pp where that fuction has other functions in it?

  23. 23

    Call variables in construct function from other functions in Codeigniter

  24. 24

    How to call asynchronous recursive functions after each other?

  25. 25

    How to call random function from list of functions?

  26. 26

    How to call WordPress function from other files

  27. 27

    Call function after other functions in Dart

  28. 28

    Render countdowns on each flat list item

  29. 29

    how to call function inside reactjs render method properly?

HotTag

Archive