Как получить обратный вызов от дочернего компонента к родительскому компоненту в React Native - PullRequest
0 голосов
/ 15 февраля 2019

Я работаю над реагировать на родной, чтобы получить обратный вызов от ребенка к родителю.Ниже приведен фрагмент кода моей реализации:

MainView.js

import React, { Component } from 'react';
import {
    StyleSheet,
    View,
    Text,
    Image,
    TouchableHighlight,
    FlatList,
    Dimensions,
} from 'react-native';
import ListCell from './ListCell';
import {displayAlert} from './CustomAlert';
type Props =  {
};

let winSize = Dimensions.get('window');
export default class MainView extends Component<Props> {

_keyExtractor = (item, index) => { return(index.toString());};

  _renderItem = ({item, index}) => {
    return (<ListCell
    item={item}
    index={index}
    onPressItem={this._onPressItem}
    />);
  };
  _onPressItem = (item,index) => {
    console.log("Pressed row : "+index);
    displayAlert();
    // this.props.navigation.navigate('Detail',{item: item});
  };
    render() {
        return(
            <FlatList
        style={styles.flatListStyle}
        data={this.props.listing}
        keyExtractor={this._keyExtractor}
        renderItem={this._renderItem}
      />
        );
    }
}

Компонент ячейки списка для FlatList:

ListCell.js

import React, {PureComponent} from 'react';
import {
    StyleSheet,
    TouchableHighlight,
    View,
    Image,
    Text,
} from 'react-native'


export default class ListCell extends PureComponent {
  _onPress() {
    this.props._onPressItem(this.props.item,this.props.index);
  }
  render() {
    const item = this.props.item;
    const price = item.price_formatted.split(' ')[0];
    return (
      <TouchableHighlight
      style={styles.listCellContainer}
      onPress={this._onPress}
      underlayColor='#dddddd'>
        <View >
          <View style={styles.rowContainer}>
            <Image style={styles.thumb} source={{uri:item.img_url}}/>
            <View style={styles.textContainer}>
              <Text style={styles.price}>{price}</Text>
              <Text style={styles.title}>{item.title}</Text>
            </View>
          </View>
        </View>
      </TouchableHighlight>
    );

  }
}

этот код будет хорошо работать при объявлении в одном файле, но при разделении на два разных файла выдает ошибку, сообщающую, что this.props._onPressItem не определяется при нажатии на ячейку.

Я выполнил следующее https://medium.com/@ruthmpardee/passing-data-between-react-components-103ad82ebd17 подход, но и в этом не удалось. Любые предложения будут полезны.

Ответы [ 3 ]

0 голосов
/ 15 февраля 2019

Ваша опора вызывается onPressItem без подчеркивания.

    this.props.onPressItem(this.props.item, this.props.index);

... и вам следует передавать саму функцию в метод onPress ваших компонентов, а не возвращаемое значение.Так что ...

      onPress={() => this._onPress}

... вместо ...

      onPress={this._onPress}
0 голосов
/ 16 февраля 2019

вы можете использовать этот код для своего родительского компонента

class ParentComponent extends Component {
    onPressButtonChildren(data){
      console.log(data)
      //press button chilldre  
    }
    render(){
      return(
        <ListChildren data={this.props.data} fnPressButton={this.onPressButtonChildren.bind(this)}/>
      )
    }
}

export default ParentComponent

это код для вашего дочернего компонента, и нажмите кнопку

const ListChildren = (props) => {
  const {price, title, image} = props.item
  const onPress = () => props.fnPressButton(props.item)
  return (
    <TouchableHighlight
      style={styles.listCellContainer}
      onPress={onPress} //Try: Also bind the event () => this._onPress()
      underlayColor="#dddddd"
    >
      <View>
        <View style={styles.rowContainer}>
          <Image style={styles.thumb} source={{ uri: img_url }} />
          <View style={styles.textContainer}>
            <Text style={styles.price}>{price}</Text>
            <Text style={styles.title}>{title}</Text>
          </View>
        </View>
      </View>
    </TouchableHighlight>
  );
};

export default ListChildren
0 голосов
/ 15 февраля 2019

Получил быстрый просмотр вашего кода.Это то, что я нашел.

export default class ListCell extends PureComponent {
  _onPress() {
    this.props.onPressItem(this.props.item,this.props.index); //Change: passing prop onPressItem and calling _onPressItem
  }
  render() {
    const item = this.props.item;
    const price = item.price_formatted.split(' ')[0];
    return (
      <TouchableHighlight
      style={styles.listCellContainer}
      onPress={this._onPress} //Try: Also bind the event () => this._onPress()
      underlayColor='#dddddd'>
        <View >
          <View style={styles.rowContainer}>
            <Image style={styles.thumb} source={{uri:item.img_url}}/>
            <View style={styles.textContainer}>
              <Text style={styles.price}>{price}</Text>
              <Text style={styles.title}>{item.title}</Text>
            </View>
          </View>
        </View>
      </TouchableHighlight>
    );

  }
}
...