gpt4 book ai didi

android - 如何在 React Native 中删除 View

转载 作者:塔克拉玛干 更新时间:2023-11-03 00:35:00 25 4
gpt4 key购买 nike

我对 react 还很陌生,直到现在我仍然无法理解如何在用户单击特定按钮时删除 View 。例如,假设我有这段代码:

import React, { Component } from 'react'
import { AppRegistry, View, Text, TouchAbleOpacity } from 'react-native'

class Example extends Component {
removeView(){
// what should I do here?
}

render(){
return (
<View>
<View> // View to be removed
<TouchAbleOpacity onPress={() => this.removeView()}>
<Text>Remove View</Text>
</TouchAbleOpacity>
</View>
</View>
)
}
}

AppRegistry.registerComponent('example', () => Example);

如何以编程方式删除 View 元素?

最佳答案

尝试这样的事情:

class Example extends Component {
constructor(props) {
super(props);
this.state = {
showView: true,
}
}

removeView(){
this.setState({
showView: false,
});
}

render(){
return (
<View style={{flex: 1, alignItems: 'center', justifyContent: 'center' }}>
{this.state.showView && (
<View style={{backgroundColor: 'red', height: 100, width: 100 }}> // View to be removed
<TouchableHighlight onPress={() => this.removeView()}>
<Text>Remove View</Text>
</TouchableHighlight>
</View>
)}
</View>
)
}
}

关于android - 如何在 React Native 中删除 View ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40088734/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com