2016-02-26 50 views
6

我用onPress“handler”渲染了下列类型的列表。如何将组件引用传递给onPress回调?

enter image description here

我已经意识到,onPress处理程序是无用的,因为我无法获得比赛按下的参考。我得到ref是没有定义的错误。

var races = Engine.possibleRaces; 

function racePressed(ref) { 
    console.log("REF: " + ref); // is never called 
} 

var races = Engine.possibleRaces; 
var rowViews = []; 
for (var i = 0; i < races.length; i++) { 
    rowViews.push(
    <Text 
     ref={i} 
     onPress={() => racePressed(ref)} // ref is not defined 
     style={styles.raceText}>{races[i].text} 
    </Text> 

); 
} 

<View style={{width: Screen.width, flexDirection:'row', flexWrap: 'wrap', alignItems: "center"}}> 
     {rowViews} 
</View> 

我也尝试着我作为参数传递。它不起作用,因为它在迭代结束后具有值。基本上它具有比赛价值。

onPress={() => racePressed(i)} // Same as races.length 

编辑:

我做分离问题下面的程序。

'use strict'; 
import React, { 
    AppRegistry, 
    Component, 
    StyleSheet, 
    Text, 
    View 
} from 'react-native'; 

let texts = [{text: "Click wrapper text1"}, {text: "Click wrapper text2"}]; 

class sandbox extends Component { 

    rowPressed(ref, i) { 
    console.log("rowPressed: " + ref + " " + i) 
    } 

    render() { 

    var rows = []; 

    for (var i = 0; i < texts.length; i++) { 
     rows.push(
     <Text 
      ref={i} 
      onPress={() => this.rowPressed.bind(this, i)} 
      style={styles.raceText}>{texts[i].text} 
     </Text> 
    ); 
    } 

    return (
     <View style={styles.container}> 

     <View> 
      {rows} 
     </View> 

     </View> 
    ); 
    } 
} 

const styles = StyleSheet.create({ 
    container: { 
    flex: 1, 
    justifyContent: 'center', 
    alignItems: 'center' 
    } 
}); 

AppRegistry.registerComponent('sandbox',() => sandbox); 

回调不叫,丢除了没有错误以下警告

[tid:com.facebook.React.JavaScript] Warning: Each child in an array or iterator should have a unique "key" prop. Check the render method of `sandbox`. See https://fb.me/react-warning-keys for more information. 

回答

11

我建议你在构造函数中绑定该函数,如..

constructor(props){ 
    super(props); 
    //your codes .... 

    this.rowPressed = this.rowPressed.bind(this); 
} 

这会绑定this rowPressed函数的上下文,如您所期望的那样,从而使您的代码正常工作。

另外,为了摆脱警告信息,您应该为每个<Text>元素提供独特的key属性,在代码中使用<Text key={i} ....>应该可行。

+0

Chaining key ref来解决警告信息问题! – Markku

+0

你能分享更多的代码吗?我应该如何更改以下代码以访问“this”? '渲染:函数(){ 功能rowPressed(ID){// 无法读取未定义 this.state.races的特性 '状态'[ID] .chosen = this.state.races [ID] .chosen ; } this.rowPressed = this.rowPressed.bind(this); ... onPress = {()=> rowPressed(i)}' – Markku

3

我尝试做同样的事情,发现这个解决方案:

render() { 
    var _data = texts; 
    return (
     <View style={styles.container}> 
     { _data.map(function(object,i) { 
      return (
       <Text ref={i} 
        onPress={() => this.rowPressed(i)} 
        style={styles.raceText}>{texts[i].text} 
       </Text>); 
      }) 
     } 
     </View> 
    ); 
} 

干杯

+1

我不能在“this.rowPressed(i)”中使用“this”表达式,否则我会得到“Can not read property'rowPressed'of undefined”。我只需要使用rowPressed并正确调用函数。现在我遇到了rowPressed函数的问题。我无法访问this.state了。 '函数rowPressed(ID){ this.state ... //无法读取的不确定 财产“状态”}' – Markku

+0

以下似乎工作,但由于某种原因,感觉没有被正确地完成。 'var that = this; 函数rowPressed(id){ that.state.races [id] .chosen =!that.state.races [id] .chosen;' – Markku