如何在React Native中从子组件回调到父组件

问题描述 投票:1回答:3

我一直在使用react-native来获得从child到parent的回调。以下是我的实现的代码片段:

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的Cell Component列表是:

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方法,但没有成功,任何建议都会有所帮助。

javascript react-native callback react-native-flatlist react-native-component
3个回答
2
投票

快速查看代码。这是我发现的。

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>
    );

  }
}

1
投票

您的道具名为onPressItem,没有下划线。

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

...并且您应该将函数本身传递给组件onPress方法,而不是返回值。那样做......

      onPress={() => this._onPress}

...代替...

      onPress={this._onPress}

1
投票

您可以像这样使用此代码为您的父组件

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
© www.soinside.com 2019 - 2024. All rights reserved.