2017-05-31 73 views
0

我的功能onPress回报undefined is not a objectTouchableHighlight onPress不工作

这是我的代码:

import React, {Component} from 'react'; 
 
import {Image, Platform, StatusBar, ListView, TouchableHighlight} from 'react-native'; 
 
import {connect} from 'react-redux'; 
 
import {Actions} from 'react-native-router-flux'; 
 
import { 
 
    Container, 
 
    Content, 
 
    Text, 
 
    Icon, 
 
    View, 
 
    Left, 
 
    Right, 
 
    Header, 
 
    Body, 
 
    Title, 
 
    Animated 
 

 
} from 'native-base'; 
 

 
import styles from './styles'; 
 

 

 
class Championnat extends Component { 
 

 
    constructor(props) { 
 
     super(props); 
 
     const ds = new ListView.DataSource({rowHasChanged: (r1, r2) => r1 !== r2}); 
 
     this.state = { 
 
      dataSource: ds.cloneWithRows([ 
 
       {slug : 'team', name : 'Liqgue1'}, 
 
       {slug : 'worldcup', name : 'Coupe du monde'}, 
 
      ]), 
 
     }; 
 
    } 
 

 
    pressRow(data) { 
 

 
     console.log(data); 
 

 
    } 
 

 
    renderRow(data){ 
 
     return (
 
      <TouchableHighlight onPress={()=> this.pressRow(data)}> 
 
       <View style={styles.lstView}> 
 
        <Image style={styles.lstPicto} /> 
 
        <Text style={styles.lstText}>{data.name}</Text> 
 
        <Icon name="angle-right" right style={styles.lstIcon} /> 
 
       </View> 
 
      </TouchableHighlight> 
 

 
     ) 
 
    } 
 

 
    render() { 
 
     return (
 
      <Container style={styles.container}> 
 

 
       <Header style={styles.header}> 
 
        <Left> 
 
         <Button transparent onPress={() => Actions.pop()}> 
 
          <Icon active name="angle-left"/> 
 
         </Button> 
 
        </Left> 
 
        <Body> 
 
         <Title>Choisir un championnat</Title> 
 
        </Body> 
 
        <Right></Right> 
 
       </Header> 
 

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

 
         dataSource={this.state.dataSource} 
 
         renderRow = {this.renderRow.bind(this)} 
 

 
        ></ListView> 
 
       </View> 
 

 
      </Container> 
 

 
     ) 
 
    } 
 
} 
 

 
export default connect()(Championnat);

回答

0

里面的你constructor你需要bind()你的功能,或使用箭头功能。

constructor() { 
    // your code 
    this.pressRow = this.pressRow.bind(this); 
} 

否则,请将您的函数声明为箭头函数。

pressRow = (data) => { 
    // your code 
} 
+0

感谢的很多你suggeste,但对于你回答, – user3646875

0

变化renderRow(data)如箭头功能类似下面片断

renderRow = (data) => { 
    // your logic 
} 

,并确保你pressRow功能应该是箭头功能

pressRow = (data) => { 
      console.log(data); 
} 

希望这会工作。 :)

+0

感谢。我们可以有整个组件代码片段。这将有助于发现问题 – user3646875

+0

没有对不起相同PB ... – user3646875

+0

可以请你给我你的问题更多信息它不工作...同样PB – Moorthy

0

试试这个..它应该工作。当你呈现一个列表视图,你们每个数据属于一个唯一的ID ..

pressRow(event) { 
 
    console.log(event); 
 
} 
 

 
renderRow(data, rowID){ 
 
    return (
 
    <TouchableHighlight key={rowID} onPress={()=> this.pressRow(rowID)}> 
 
    <View style={styles.lstView}> 
 
     <Image style={styles.lstPicto} /> 
 
     <Text style={styles.lstText}>{data.name}</Text> 
 
     <Icon name="angle-right" right style={styles.lstIcon} /> 
 
     </View> 
 
    </TouchableHighlight> 
 

 
     ) 
 
}

+0

Arf,它不适合我... – user3646875

0

这是我的全部代码: 在renderRow,如果我评论Button一个分解TouchableHighlight它不起作用,如果我评论TouchableHighlight和分解Button,它的工作。

import React, {Component} from 'react'; 
import {Image, Platform, StatusBar, ListView, TouchableHighlight} from 'react-native'; 
import {connect} from 'react-redux'; 
import {Actions} from 'react-native-router-flux'; 
import { 
    Container, 
    Content, 
    Text, 
    Item, 
    Input, 
    Button, 
    Icon, 
    View, 
    Left, 
    Right, 
    Header, 
    Body, 
    Title, 
    Spinner, 
    Animated 

} from 'native-base'; 

import styles from './styles'; 
import * as competition from '../../../services/competition'; 


class ContentLoaded extends Component { 
    render() { 
     return (
      <Text>Hello {this.props.name}!</Text> 
     ); 
    } 
} 

class Competition extends Component { 

    constructor(props) { 

     super(props); 
     this.pressRow = this.pressRow.bind(this); 


     this.state = { 
      dataSource: new ListView.DataSource({ 
       rowHasChanged: (row1, row2) => row1 !== row2, 
      }), 
      loaded: false, 
     }; 

     this.fetchData(); 


    } 

    /** 
    * Cherche les competitions 
    */ 
    fetchData = function() { 

     competition.getAll() 
      .then((data) => { 

       console.log(data); 

       this.setState({ 
        dataSource: this.state.dataSource.cloneWithRows(data), 
        loaded: true, 
       }); 

      }) 
      .catch((exception) => { 

       console.log('competition controller 47', exception); 

      }); 

    } 

    /** 
    * Click sur une competiotn 
    * @param data 
    */ 
    pressRow = (data, rowId) => { 

     console.log(data); 
     console.log(rowId); 


    } 

    /** 
    * render d'une ligne 
    * @param data 
    * @returns {XML} 
    */ 
    renderRow(data, rowId){ 
     return (
      //<TouchableHighlight key={rowID} style={styles.lstView} onPress={()=> this.pressRow(rowId)}> 

       <Button style={styles.lstView} onPress={()=> this.pressRow(data, rowId)}> 
        <Image style={styles.lstPicto} /> 
        <Text style={styles.lstText}>{data.name}</Text> 
        <Icon name="angle-right" right style={styles.lstIcon} /> 
       </Button> 

      //</TouchableHighlight> 

     ) 
    } 

    /** 
    * Affichage conditionnek 
    * @returns {XML} 
    * @constructor 
    */ 
    ContentLoaded() { 

     if (!this.state.loaded) { 
      return <Text> 
       Loading movies... 
      </Text> 
     } 

     return <ListView 

      dataSource={this.state.dataSource} 
      renderRow = {this.renderRow.bind(this)} 

     ></ListView> 


    } 

    /** 
    * Render 
    * @returns {XML} 
    */ 
    render() { 

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

       <Header style={styles.header}> 
        <Left> 
         <Button transparent onPress={() => Actions.pop()}> 
          <Icon active name="angle-left"/> 
         </Button> 
        </Left> 
        <Body> 
         <Title>Choisir un championnat</Title> 
        </Body> 
        <Right></Right> 
       </Header> 

       <View style={{flex: 1}}> 
        {!this.state.loaded ? (
         <Spinner size="small" color="#000000" /> 
        ) : (
         <ListView 

          dataSource={this.state.dataSource} 
          renderRow = {this.renderRow.bind(this)} 

         ></ListView> 
        )} 
       </View> 

      </Container> 

     ) 
    } 
} 

export default connect()(Competition); 
+0

你pressRow需要收到两个参数,但里面TouchableHighlight你也发送rowId ..尝试拷贝this.pressRow(数据,rowId)里面的按钮onPress ... –

相关问题