更改图像点击In React本地

发布于 2025-02-01 20:27:55 字数 1332 浏览 3 评论 0原文

我正在尝试创建一个应用程序,该应用在单击图像时更改为许多图像之一。我使用了可触摸的不透明度,可以在单击时使图像显示警报。我只是无法将其更改为文件中的许多其他人。

到目前为止,这是我所有的代码:

import React from 'react';

import { Component, Stylesheet, useState, TouchableOpacity, Button, View, Text, Image, ScrollView, TextInput, Alert } from 'react-native';

// main part of the app
const App = () => {
  

  var array = [require("./cards/card.png"), require("./cards/card2.png")]
  var x = 0
  

  //onclick function
  const handlePress = () => {
    //some logic
    alert("help")
    x+=1
  }
  
  // what shows up on the app
  return (

    <ScrollView>

      <View>
          <Text>{array[x]}</Text>
          <Text>{x}</Text>
          <View style={{ flexDirection: 'row', justifyContent: 'center' }}>
          
          <TouchableOpacity
            onPress={(handlePress)}
          >

            <Image
              style={{ 
                width: 300,
                height: 300,
              }}
              resizeMode="contain"
              source={
                array[x]
              }
            />

          </TouchableOpacity>

          </View>

      </View>

    </ScrollView>
    
  );
}

export default App;

我希望将卡更改为“卡片”文件夹中的其他图像。我该怎么做才能使其动态并将其更改为文件夹中的其他任何卡片?

I am trying to create an app that changes to one of many images when the image is clicked. I have used touchable opacity and can make the image show an alert when clicked. I just can't get it to change to one of the many others in the file.

Here is all my code so far:

import React from 'react';

import { Component, Stylesheet, useState, TouchableOpacity, Button, View, Text, Image, ScrollView, TextInput, Alert } from 'react-native';

// main part of the app
const App = () => {
  

  var array = [require("./cards/card.png"), require("./cards/card2.png")]
  var x = 0
  

  //onclick function
  const handlePress = () => {
    //some logic
    alert("help")
    x+=1
  }
  
  // what shows up on the app
  return (

    <ScrollView>

      <View>
          <Text>{array[x]}</Text>
          <Text>{x}</Text>
          <View style={{ flexDirection: 'row', justifyContent: 'center' }}>
          
          <TouchableOpacity
            onPress={(handlePress)}
          >

            <Image
              style={{ 
                width: 300,
                height: 300,
              }}
              resizeMode="contain"
              source={
                array[x]
              }
            />

          </TouchableOpacity>

          </View>

      </View>

    </ScrollView>
    
  );
}

export default App;

The other images that I want the card to change to are in the cards folder. What do I do to make it dynamic and change it to any of the other cards in the folder?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

凶凌 2025-02-08 20:27:55

为了更改屏幕上安装的图像或任何信息,React必须重新渲染屏幕并放置所需的正确信息。

为此,您应该使用React状态 https://reactnative.dev/docs/state

您的代码可能是这样:

import React from 'react';

import { Component, Stylesheet, useState, TouchableOpacity, Button, View, Text, Image, ScrollView, TextInput, Alert } from 'react-native';

// main part of the app
const App = () => {
  
  const [card, setCard] = useState(0); //initial state

  var array = [
    "./cards/card.png", 
    "./cards/card2.png"
  ]
  

  //onclick function
  const handlePress = () => {
    //some logic
    //set card state to the next index
    setCard( current => current + 1);

    //so everytime you click function the state will change and this re-render your component with the new data.
  }
  
  // what shows up on the app
  return (

    <ScrollView>

      <View>
          <Text>{array[card]}</Text>
          <View style={{ flexDirection: 'row', justifyContent: 'center' }}>
          
          <TouchableOpacity
            onPress={(handlePress)}
          >

            <Image
              style={{ 
                width: 300,
                height: 300,
              }}
              resizeMode="contain"
              source={ require(array[card]) }
            />

          </TouchableOpacity>

          </View>

      </View>

    </ScrollView>
    
  );
}

export default App;

in order to change image or any information in a screen that have been mounted, React have to re-render the screen and put the right information that you want.

To achieve this, you should use React states https://reactnative.dev/docs/state

Your code could be like this:

import React from 'react';

import { Component, Stylesheet, useState, TouchableOpacity, Button, View, Text, Image, ScrollView, TextInput, Alert } from 'react-native';

// main part of the app
const App = () => {
  
  const [card, setCard] = useState(0); //initial state

  var array = [
    "./cards/card.png", 
    "./cards/card2.png"
  ]
  

  //onclick function
  const handlePress = () => {
    //some logic
    //set card state to the next index
    setCard( current => current + 1);

    //so everytime you click function the state will change and this re-render your component with the new data.
  }
  
  // what shows up on the app
  return (

    <ScrollView>

      <View>
          <Text>{array[card]}</Text>
          <View style={{ flexDirection: 'row', justifyContent: 'center' }}>
          
          <TouchableOpacity
            onPress={(handlePress)}
          >

            <Image
              style={{ 
                width: 300,
                height: 300,
              }}
              resizeMode="contain"
              source={ require(array[card]) }
            />

          </TouchableOpacity>

          </View>

      </View>

    </ScrollView>
    
  );
}

export default App;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文