Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

React Native loop this

when I put onPress in a map loop, it doesn't work. how to fix it?

var PageOne = React.createClass({
  _handlePress() {
    this.props.navigator.push({id: 2,});
  },
  render () {
      return (
        <View>          
          <TouchableOpacity onPress={this._handlePress}> //work here 
          <Text> One </Text>
          </TouchableOpacity>
          <View style={styles.albums}>
          {
            list.map(function(item, index){
              return (
                <TouchableOpacity key={index} onPress={this._handlePress}> //doesn't work hehre
                  <Text>{item}</Text>
                </TouchableOpacity>
              )
            })
          }
            </View>
      </View>
      );
  }
});
like image 519
Alien Avatar asked Jan 18 '16 17:01

Alien


People also ask

Can you use for loop in React?

A JavaScript for loop is a technique that allows you to repeat the same steps on a group of items, usually an array. In React application, for loop can be used in a number of different ways. However, there are 2 preferred methods when it comes to writing for loops in React JS.

How do you loop in react native?

To loop and render elements in React Native, we can use the JavaScript array map method. to call arr. map with a callback to return the Text component with the entry a as the text. We set the key prop to a unique value for each entry so that React can identify the rendered components.

How do you loop through an array of objects in react native?

To loop through an array of objects in React:Use the map() method to iterate over the array. The function you pass to map() gets called for each element in the array. The method returns a new array with the results of the passed in function.

Can we use for loop inside JSX like this?

Yes you an create a for loop inside react JSX with the help of function callback approach. In fact, you can run any code within JSX using this approach. But it is advised not to use it because every time JSX renders, a new function will be instantiated.


1 Answers

this is referring to the wrong context, you need the scope to be lexically bound, which is what the fat arrow function will do for you.

Try calling your function like this:

onPress={ () => this._handlePress() }

Also, you need to bind the map function to the correct context like this:

<View style={styles.albums}>
  {
    list.map(function(item, index){
       return (
         <TouchableOpacity key={index} onPress={() => this._handlePress()}> 
           <Text>{item}</Text>
          </TouchableOpacity>
       )
     }.bind(this))
   }
</View>

Or like this:

<View style={styles.albums}>
  {
    list.map((item, index) => {
       return (
         <TouchableOpacity key={index} onPress={() => this._handlePress()}> 
           <Text>{item}</Text>
         </TouchableOpacity>
       )
     })
   }
</View>

I set up a working project here.

https://rnplay.org/apps/_PmG6Q

like image 86
Nader Dabit Avatar answered Oct 27 '22 09:10

Nader Dabit