react-native - 在本机反应中隐藏/显示组件
问题描述
我想在我 onPress 自定义组件时显示。自定义组件是一个包装在多个 and 中的组件。
我的意思是,当 onPress 的时候,当然是包裹在 .
像这样,
import React from 'react';
import {
SafeAreaView,
Text,
FlatList,
View,
StyleSheet,
StatusBar,
Image,
ScrollView,
TouchableHighlight,
TouchableOpacity,
Button,
} from 'react-native';
import HeartButtons from './buttons';
import Item from './items';
const App = () => {
const renderItem = ({item}: any) => (
<View style={{backgroundColor: colors.white}}>
<Item title={item.title} />
<TouchableOpacity activeOpacity={0.5} style={styles.buttonHeartStyle}>
<Image source={heart} />
</TouchableOpacity>
<TouchableOpacity activeOpacity={0.5} style={styles.buttonCheckStyle}>
<Image source={check} />
</TouchableOpacity>
</View>
);
return (
<SafeAreaView>
<FlatList
data={DATA}
renderItem={renderItem}
keyExtractor={item => item.id}
/>
<TouchableOpacity
activeOpacity={0.7}
onPress={clickHandler}
style={styles.touchableOpacityStyle}>
</TouchableOpacity>
</SafeAreaView>
);
};
export default App;
它是代码的一部分。当我单击<Item title={item.title} />
时,应该在 renderItem 函数中显示。
我应该怎么办?
我已经一个星期没吃过了。如果你能帮助我,我将不胜感激。
先感谢您。
解决方案
只需将您renderItem
的修改为
const renderItem = ({ item }: any) => {
const [isActive, setActive] = useState(false);
return (
<View style={{ backgroundColor: colors.white }}>
<TouchableOpacity onPress={() => setActive(!isActive)}>
<Item title={item.title} />
</TouchableOpacity>
{
isActive &&
<>
// Add here what you want to show
</>
}
</View>
);
};
推荐阅读
- c++ - 如何创建 DIB、设置位并附加到 CImageList
- python - 如何在不覆盖现有密钥的情况下向 Dynamodb 映射添加新密钥?
- reactjs - Axios @ React:错误:请求失败,PATCH 状态码为 400
- django - 我正在寻找一个库,它允许 Django 用户对 API 进行 OAuth 身份验证,并且 Django 可以访问他们的数据
- node.js - 如何在 Firestore 中调用 Async 和 Await
- swift - 调整大小时 SwiftUI macOS 极端滞后窗口
- api - REST API:验证 GET 请求中的请求参数?
- python - Plotly - 如何在折线图的 Y 轴上添加行数?
- c - 为什么分配给我的 malloc 的内存会被覆盖?
- mysql - MySQL Init SQL 文件没有被挂载