我有一個(gè)touchableOpacity區(qū)域,里面有圖像。我想在用戶單擊圖像時(shí)進(jìn)行設(shè)置,將touchableOpacity的不透明度設(shè)置為0.2,然后執(zhí)行獲取,其結(jié)果將為0或1。如果為0,則將圖像更改為黑白圖像,否則為彩色圖像。但是我無法做到這一點(diǎn)。找不到我單擊touchableOpacity區(qū)域時(shí),console.log在控制臺(tái)中打印其值但未設(shè)置state.opacity的原因。import React, { Component } from 'react';import {Image, TouchableOpacity, View, Text, StyleSheet, StatusBar } from 'react-native';import styles from './Estilos';export default class LightsContainer extends Component {constructor() {super(); this.state = { opacity: 0.9 };}onPress = () => { fetch("http://192.168.0.161/switch.php?port=1") .then(response => response.text()) .then((dataStr) => { console.log(dataStr); if (dataStr == 1){ this.setState({opacity: 0.9}); console.log("si"); } else { this.setState({opacity: 0.2}); console.log("no"); } }); console.log(this.state);});}}
為什么當(dāng)我單擊按鈕后,在獲取Prmise和this.setState之后才打印控制臺(tái)日志
白板的微信
2021-03-31 11:11:22