【发布时间】:2020-08-09 03:34:20
【问题描述】:
当我单击“显示元素”按钮时,我想将状态更改为 true 当我点击“隐藏元素”按钮时,我想将状态更改为 false
但是当我第一次单击“显示元素”按钮时,它不会更改为 true,并且需要我第二次单击按钮才能将状态更改为 true。当我点击“隐藏元素”按钮时也是如此
这是我的代码:
import * as React from 'react';
import { View, Text, StyleSheet, Platform, AsyncStorage, TouchableHighlight, ScrollView, SafeAreaView, Button } from 'react-native'
class Base extends React.Component {
constructor(props) {
super(props);
this.state = {
isForShowVar: true
}
}
render() {
return(
<View>
<Button
style={{marginTop:50, width:150}}
title="Hide Element"
onPress={()=>{ alert(this.state.isForShowVar); this.setState({isForShowVar: false}); }}
/>
<Button
style={{marginTop:50, width:150}}
title="Show Element"
onPress={()=>{ alert(this.state.isForShowVar); this.setState({isForShowVar: true}); }}
/>
</View>
)
}
}
export default Base;
你可以在here测试代码
当我第一次点击按钮时如何改变状态?
谢谢
【问题讨论】:
标签: react-native state