React Native - 在按钮上隐藏和显示组件点击动画
问题描述:
朋友我有隐藏和显示在运行时当用户点击按钮的组件(视图)必须隐藏并再次点击它应该显示的按钮。React Native - 在按钮上隐藏和显示组件点击动画
mycode的:
constructor(props) {
super(props);
this.state = {
isModalVisible : false,
};
}
callFunc(){
if(this.isModalVisible){
this.setState({isModalVisible:false});
}else{
this.setState({isModalVisible:true});
}
}
render() {
return (
<View style = {styles.companyScroll}>
<Button
onPress={this.callFunc}
title="Learn More"
color="#841584"
accessibilityLabel="Learn more about this purple button"
/>
{this.state.isModalVisible && <Picker style ={{backgroundColor : 'white'}}
selectedValue={this.state.language}
onValueChange={(itemValue, itemIndex) => this.setState({language: itemValue})}>
<Picker.Item label="Java" value="java" />
<Picker.Item label="JavaScript" value="js" />
</Picker>
</View>
)
}
我要像下面的图片。
答
您还应该使用this.state.isModalVisible
而不是this.isModalVisible
从callFunc()
。
答
你错过了功能绑定:
constructor(props) {
super(props);
this.callFunc = this.callFunc.bind(this);
...
}
没有它,callFunc
不会在this
对象的范围,并有将其状态的访问权限。
而@ @AlexanderIgnácz说,那里有一个错字 - 应该将this.isModalVisible
更改为this.state.isModalVisible
。也许晚了,但我也想说,并为此目的来完成这个答案。
答
在构造函数中添加
this.callFunc = this.callFunc.bind(this);
在onPress与
{()=>{ this.callFunc(); }
你可以在这里看到一个豆蔻例如更换:
https://snack.expo.io/HkxpgSlPZ
@ +
谢谢。工作中。但想要动画。任何想法。 –
什么样的动画,不透明或下拉或其他? – Val
你应该使用动画组件。 https://facebook.github.io/react-native/docs/animated.html –