我正在开发react原生应用程序。对于滑块部分,我放了"Snap carousel“插件。我的滑块显示了多个项目。按时间计算滑块项目与除以窗口宽度/5。现在我有5个滑块项目在显示。
问题是,我需要显示活动滑块项目的滑块文本和更改背景颜色。
在渲染项中,我不能使用state或props值。如何在render item方法中检查当前索引值?
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}文本。
发布于 2020-02-27 15:59:11
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>https://stackoverflow.com/questions/50564501
复制相似问题