簡體   English   中英

是否可以在沒有構造函數的情況下更改圖像? react-native

[英]Is it possible to change the image without constructor? react-native

我希望在我的 function 中單擊沒有構造函數的 onpress 后,我的圖片更改為另一張圖片。

我的圖像代碼:

 <Image source={require("./assets/images/imgas.png")} resizeMode="contain" style={styles.ss} ></Image>

單擊另一個圖像時如何打開它? 因為我使用的是 function,所以無法使用構造函數。

有什么想法嗎?

嘗試這樣做,然后調用setUri來更改圖像

const image = require("./assets/images/imgas.png")

const [uri, setUri] = useState(image);

<Image source={{uri}} resizeMode="contain" style={styles.ss} />
export default class Image extends Component {
  constructor(props) {
    this.state = {
     uri: '',
     }
  }

  handleButtonPress = () => {
    this.setState({
      uri: './assets/images/imgas.png',
    });
  }

  render() {
    return (
      ...
      <Button
        onPress={this.handleButtonPress}
        ...
      />
      ...
    );
  }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM