javascript - React-Native-snap-carousel 检查条件与状态值?

标签 javascript arrays react-native react-native-flatlist react-native-snap-carousel

我正在研究 React native 应用程序。对于 slider 部分,我放置了“Snap carousel”插件。我的 slider 显示多个项目。到时候用除以窗口宽度/5 来计算 slider 项。现在我显示了 5 个 slider 项目。

问题是,我需要为事件 slider 项显示 slider 文本并更改背景颜色。

在渲染项目中我不能使用状态或 Prop 值。如何在渲染项方法中检查当前索引值?

constructor(props) {
    super(props);
    this.state = {
        activeSlide:0
    }
}
_renderItem ({item, index, currentIndex}) {
    const self = this;
    return (
        <View style={styles.slide}>
            <View style={stylesSelect.carouselImg}>
                <Image 
                source={item.src}
                resizeMode='cover' 
                />
            </View>
            <Text>
                {item.title} 
            </Text>
        </View>
    );
}
render() {
    const { data } = this.props;
    return (
        <View style={styles.container}>
            <Carousel
                ref={(c) => { this._carousel = c; }}
                data={data}
                renderItem={this._renderItem}
                sliderWidth={width}
                itemWidth={Math.round(width/5)}
                sliderHeight={height}
                itemHeight={height}
                inactiveSlideScale={0.8}
                inactiveSlideOpacity={0.7}
                loop={true}
                onSnapToItem={(index) => this.setState({ activeSlide: index }) }
            />
        </View>
    )
}

我只想显示事件 slider 项目的 {item.title} 文本。

最佳答案

renderItem = data =>
        <View>
            <TouchableOpacity >
                {/* {console.log("==================================")}
                {console.log(data)} */}
            <ImageLoad
                style={{ height: 200, width: itemWidth, borderRadius: theme.sizes.itemRadius }}
                placeholderSource={require('./images/logo.png')}
                source={{ uri: data.item.thumbnail }}
                resizeMode={'contain'}
                isShowActivity={true}
                borderRadius={theme.sizes.itemRadius}
                placeholderStyle={{ height: 200, width: itemWidth, borderRadius: theme.sizes.itemRadius }}
            />
            <View style={styles.featuredDiv}><Text style={styles.title}>{data.item.title}</Text></View>
            </TouchableOpacity>
        </View>

关于javascript - React-Native-snap-carousel 检查条件与状态值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50564501/

相关文章:

javascript - 在 G 标签中包装现有的 SVG 元素

php - json_encode() 什么都不返回

javascript - Jquery:使用 countdownTimer 获取意外的 token 非法

javascript - 从列表中选择 4 张图像

C# datagridview 列到一个数组

javascript - 如何防止屏幕滚动到 x :0, y :0 when an input loses focus?

reactjs - E/未知 :React: Exception in native call

java - 如何传入一个类来创建一个类数组?

java - 没有初始大小的字符串数组给出空指针异常

react-native - 我如何在 native react 中获取路线参数