chuikoffru
@chuikoffru
Full Stack JS Developer

Как выполнить клик по элементу ListView в React Native?

Здравствуйте! Пишу первое приложение на ReactNative, получил данные с сервера, вывел их с помощью ListView, но при клике на элемент, получаю ошибку:

44b6a68633374713a5f829ad4c2376ec.png

Вот код:

import React, { Component } from 'react';
import * as cnf from './config.js';
import {
  AppRegistry,
  Text,
  Image,
  ListView,
  TouchableHighlight,
  View
} from 'react-native';

export default class Users extends Component {
    constructor(props) {
    super(props);
    
    this._onPress = this._onPress.bind(this);
    
    this.state = {
      users : new ListView.DataSource({
        rowHasChanged: (row1, row2) => row1 !== row2,
      }),
      loaded: false
    }
    
  }

componentDidMount() {
    fetch(cnf.REQUEST_URL + 'publications/users')
      .then((response) => response.json())
      .then((responseData) => {
        this.setState({
          users: this.state.users.cloneWithRows(responseData.users),
          loaded: true
        });
      })
      .catch((error) => {
        console.warn(error);
      })
      .done();
  }

render() {
    if (!this.state.loaded) {
      return this.renderLoadingView();
    }
    return(
      <View>
        <View style={styles.blockTitle}>
          <Text style={styles.actionTitle}>Выберите пользователя:</Text>
        </View>
        <ListView
          style={styles.listView}
          dataSource={this.state.users}
          renderRow={this.rowUser}
        />
      </View>
    );
  }
  
  _onPress() {
    console.log('fd');
  }
  
  rowUser(user) {
    if(user.roles[0] == 'user') {
      return (
        <TouchableHighlight onPress={() => this._onPress()}>
          <View style={styles.container}>
            <Image
              source={{uri: user.profile.avatar}}
              style={styles.avatar}
            />
            <View style={styles.rightContainer}>
              <Text style={styles.name}>{user.profile.fio}</Text>
            </View>
        </View>
      </TouchableHighlight>
      )
    } else {
      return null;
    }
      
  }

}


Помогите пожалуйста понять почему не происходит вызова функции.
  • Вопрос задан
  • 443 просмотра
Решения вопроса 1
chuikoffru
@chuikoffru Автор вопроса
Full Stack JS Developer
Оказывается решалось все очень просто.

Убираем это:
this._onPress = this._onPress.bind(this);

И меняем немного код здесь:
<ListView
          style={styles.listView}
          dataSource={this.state.users}
          renderRow={this.rowUser.bind(this)}
        />


И все прекрасно отрабатывается.
Ответ написан
Комментировать
Пригласить эксперта
Ваш ответ на вопрос

Войдите, чтобы написать ответ

Войти через центр авторизации
Похожие вопросы