javascript - React Native 中的图像更改 onPress
问题描述
import React, { useState } from 'react'
import { Text, View, StyleSheet, Image, TouchableOpacity, Platform } from 'react-native'
import HeaderComponent from '../../components/HeaderComponent'
import NavigationFooter from '../../components/NavigationFooter'
const ReportScreen = () => {
return (
<View style={styles.viewcontainer}>
<HeaderComponent title="Taak" />
<Text style={styles.subtext}>Selecteer het taak type</Text>
我想更改下面的图像onPress
,TouchableOpacity
但我不知道该怎么做useState
<TouchableOpacity>
<View style={styles.graycontainer}>
<Image style={styles.grayimg} source={require('../../../assets/report/gray.png')} />
<Text style={styles.graytext}>Grijs</Text>)
</View>
</TouchableOpacity>
</View>
)
}
解决方案
你可以像这样使用 Pressable:
import { View, Image, StyleSheet, Pressable } from 'react-native';
import imagesOn from '../../../assets/report/grayOn.png'
import imagesOff from '../../../assets/report/grayOFF.png'
let [flag, setflag] = React.useState(true);
let changeImage = () => setFlag(previousState => !previousState);
let imagePath= flag ? imagesOn : imagesOff
------
-----
<Pressable onPress={ () => changeImage() }>
<View style={styles.graycontainer}>
<Image style={styles.grayimg} source={imagePath} />
<Text style={styles.graytext}>Grijs</Text>)
</View>
</Pressable>
推荐阅读
- css - 访问 SASS 模块中的自定义属性
- vue.js - FeathersJS + VueJS + Kerberos:怎么做?
- php - Rss 提要未在浏览器中显示为提要
- google-cloud-platform - 如何保留公共 IP(静态 IP)来执行谷歌数据流作业,以便我可以将源应用程序中的 IP 列入白名单?
- javascript - 显示多组按钮的当前按钮
- python - 哪些 python 可视化库支持浏览器中的周期性回调?
- rust - 使用 Warp 的 and_then 时未实现未来特征
- next.js - Next.js 应用程序部署为具有一些托管服务提供商限制的 ssr 应用程序
- heroku - “heroku local”是否提供 HTTPS 代理?
- python - Python Opencv imwrite 与土耳其字符