Javascript 是否将本机快照转盘检查条件与状态值进行反应?
我正在开发react原生应用程序。对于滑块部分,我添加了“Snap carousel”插件。我的滑块显示多个项目。按时间用除以窗口宽度/5计算滑块项。现在我有5个滑块项目显示 问题是,我需要显示活动滑块项目的滑块文本并更改背景颜色 在渲染项中,我不能使用状态或道具值。如何在render item方法中使用当前索引值进行检查Javascript 是否将本机快照转盘检查条件与状态值进行反应?,javascript,arrays,react-native,react-native-flatlist,react-native-snap-carousel,Javascript,Arrays,React Native,React Native Flatlist,React Native Snap Carousel,我正在开发react原生应用程序。对于滑块部分,我添加了“Snap carousel”插件。我的滑块显示多个项目。按时间用除以窗口宽度/5计算滑块项。现在我有5个滑块项目显示 问题是,我需要显示活动滑块项目的滑块文本并更改背景颜色 在渲染项中,我不能使用状态或道具值。如何在render item方法中使用当前索引值进行检查 constructor(props) { super(props); this.state = { activeSlide:0 } }
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>
)
}
构造函数(道具){
超级(道具);
此.state={
动态幻灯片:0
}
}
_renderItem({item,index,currentIndex}){
const self=这个;
返回(
{item.title}
);
}
render(){
const{data}=this.props;
返回(
{this._carousel=c;}}
数据={data}
renderItem={this.\u renderItem}
滑块宽度={width}
itemWidth={Math.round(width/5)}
滑块高度={height}
itemHeight={height}
不活动滑动比例={0.8}
不活动不透明度={0.7}
循环={true}
onSnapToItem={(index)=>this.setState({activeSlide:index})}
/>
)
}
我只想显示活动滑块项目的{item.title}
文本。renderItem=data=>
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>
{/*{console.log(“====================================================”)
{console.log(数据)}*/}
{data.item.title}
请显示您的代码。@LyoshaKorogoda,请参见上文我的代码。因此,在您的\u renderItem
中,您有索引和活动索引,可以执行{this.state.activeSlide===index&&{item.title}
?我们不能在renderItem
方法中使用this.state.activeSlide
。为什么?从我看来,这只是你创建的回调。不过,您可能需要将其绑定到正确的上下文。