React native 如何在React Native中从渲染中获取var/const
我想在平面列表项目中使用动画。我创建了动画功能,以及位于渲染中的样式。但是我不能使用那种风格,因为我的renderItem函数已经超出了render,我能做些什么呢?我的意思是,我们如何在render中使用const,在render之外的其他函数中使用constReact native 如何在React Native中从渲染中获取var/const,react-native,animation,styling,React Native,Animation,Styling,我想在平面列表项目中使用动画。我创建了动画功能,以及位于渲染中的样式。但是我不能使用那种风格,因为我的renderItem函数已经超出了render,我能做些什么呢?我的意思是,我们如何在render中使用const,在render之外的其他函数中使用const state = { animation: new Animated.Value(1) }; startAnimation = () => { Animated.timing(this.st
state = {
animation: new Animated.Value(1)
};
startAnimation = () => {
Animated.timing(this.state.animation, {
toValue: 1.5,
duration: 1500
}).start();
}
_renderItem = ({item}) => {
return(
<Animated.View style={[animatedStyles]} >
<TouchableOpacity onLongPress={() => this.activateItem(item.id)} onPress={() => this.startAnimation}>
<Image source={ images[item.item_id].uri } style={{width: 60, height: 60}}/>
</TouchableOpacity>
</Animated.View>
);
};
render(){
const animatedStyles = {
transform: [
{
scale: this.state.animation
}
]
};
return(
<View style={{flex: 1}}>
<FlatList
numColumns={4}
contentContainerStyle={{flexDirection: 'column', justifyContent: 'space-between', marginRight: 10, marginLeft: 10}}
data={this.state.items}
renderItem={this._renderItem}
keyExtractor={this._keyExtractor}
/>
</View>
);
状态={
动画:新动画。值(1)
};
startAnimation=()=>{
动画。计时(this.state.animation{
toValue:1.5,
持续时间:1500
}).start();
}
_renderItem=({item})=>{
返回(
this.activateItem(item.id)}onPress={()=>this.startAnimation}>
);
};
render(){
常量animatedStyles={
转换:[
{
缩放:this.state.animation
}
]
};
返回(
);
您没有理由无法访问_renderItem函数中的this.state,因此在这种情况下,应该在此处设置它
_renderItem = ({item}) => {
return(
<Animated.View style={{ transform: [{ scale: this.state.animation]}} >
<TouchableOpacity onLongPress={() => this.activateItem(item.id)} onPress={() => this.startAnimation}>
<Image source={ images[item.item_id].uri } style={{width: 60, height: 60}}/>
</TouchableOpacity>
</Animated.View>
);
};
它工作正常,感谢您简单而有用的解释!:)
extraData={this.state.animation}