React native 使用';道具类型';

React native 使用';道具类型';,react-native,callback,react-proptypes,React Native,Callback,React Proptypes,我有画廊组件,如: import React from 'react'; ... Some import import PropTypes from 'prop-types'; import { TouchableOpacity, Image, FlatList } from 'react-native'; export default class Gallery extends React.Component { constructor(props) {

我有画廊组件,如:

import React from 'react';
... Some import
import PropTypes from 'prop-types';
import {
    TouchableOpacity,
    Image,
    FlatList
} from 'react-native';

export default class Gallery extends React.Component {
    constructor(props) {
        super(props);
        this.state = {
            selected: 0,
            data: this.props.data
        }
    }
    render() {
        return (
            <FlatList
                showsHorizontalScrollIndicator={false}
                horizontal={true}
                data={this.state.data}
                keyExtractor={(item, index) => item.id}
                renderItem={({item, index}) =>
                    <TouchableOpacity
                        disabled={this.props.overlay && this.state.selected==index}
                        // onPress={this.props.actionOnPress}
                        onPress={()=>{
                            if(this.props.overlay) {
                                this.setState({
                                    selected: index,
                                    data: [...this.props.data]
                                });
                            }
                            this.props.actionOnPress;
                        }}>
                        <Image
                            source={{uri: item.imageUrl}}
                            style={[gallery.galleryItem, this.props.overlay && this.state.selected!=index ?gallery.overlay :  null,
                            index==0 ? gallery.firstGalleryItem : ( index+1 == Object.keys(this.props.data).length ? gallery.lastGalleryItem : null )]}/>
                    </TouchableOpacity>
                }
            />
        );
    }
}
Gallery.propTypes = {
    data: PropTypes.array,
    actionOnPress: PropTypes.func,
    overlay: PropTypes.bool
}
而不是回调参数
索引

你们能帮帮我吗


谢谢,

只需使用以下参数调用函数:

onPress={() => {
  ...
  this.props.actionOnPress({ item, index });
}}
if(this.props.overlay) {
    this.setState({
        selected: index,
        data: [...this.props.data]
    });
}
onPress={() => {
  ...
  this.props.actionOnPress({ item, index });
}}