【发布时间】:2019-10-23 14:28:17
【问题描述】:
我最近加入了 React Native 项目团队。由于时间有限,没有时间深入学习 JavaScript,所以我直接继续这个项目。我的问题是如何通过单击复选框来更改片段。因此,当我单击复选框时,它会更改为另一个片段,当我单击取消选中复选框时,它会返回到前一个片段。我找到了这个React Native - CheckBox unable to uncheck the "checked" box 但这对我来说有点难以理解。
我做了什么:
export default class App extends React.Component {
constructor(props) {
super(props);
//state to manage the screen visible at a time
this.state = { val: 1 };
}
renderElement() {
//You can add N number of Views here in if-else condition
if (this.state.val === 1) {
//Return the FirstScreen as a child to set in Parent View
return <FirstScreen />;
} else if (this.state.val === 2) {
//Return the SecondScreen as a child to set in Parent View
return <SecondScreen />;
} else {
//Return the ThirdScreen as a child to set in Parent View
return <ThirdScreen />;
}
}
...
<TouchableOpacity
style={styles.button}
onPress={() => this.setState({ val: 1 })}>
<Text style={{ color: '#ffffff' }}>1st View</Text>
</TouchableOpacity>
<TouchableOpacity
style={styles.button}
onPress={() => this.setState({ val: 2 })}>
<Text style={{ color: '#ffffff' }}>2nd View</Text>
</TouchableOpacity>
<CheckBox
value = { this.state.check }
onChange={() => this.setState({ val: 2 })/>
//This is where my fragment changes
<View style={{ backgroundColor: '#ffffff' }}>
{this.renderElement()}
</View>
目前,我可以转到第二个片段。但是我不能回到第一个片段并取消选中复选框。
【问题讨论】:
-
如果您已经在按下按钮时更改片段,为什么还需要该复选框?
-
任务是用复选框来完成它。但我是用按钮来测试的