首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >带状态值的React-Native-snap-carousel检查条件?

带状态值的React-Native-snap-carousel检查条件?
EN

Stack Overflow用户
提问于 2018-05-28 18:40:35
回答 1查看 3K关注 0票数 3

我正在开发react原生应用程序。对于滑块部分,我放了"Snap carousel“插件。我的滑块显示了多个项目。按时间计算滑块项目与除以窗口宽度/5。现在我有5个滑块项目在显示。

问题是,我需要显示活动滑块项目的滑块文本和更改背景颜色。

在渲染项中,我不能使用state或props值。如何在render item方法中检查当前索引值?

代码语言:javascript
复制
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>
    )
}

我只想显示活动滑块项的{item.title}文本。

EN

回答 1

Stack Overflow用户

发布于 2020-02-27 15:59:11

代码语言:javascript
复制
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>
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/50564501

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档