如何使用状态/属性在一组图像中循环显示?

马特123

我有一个本机应用程序,我想使用包含图像的状态/属性传递对象数组。我想在按下按钮时循环显示图像。我已经为此工作了一段时间,无法弄清楚。有人介意看看吗?我是本机反应的新手,所以如果您认为我应该以不同的方式进行操作,请告诉我。谢谢!!

我已经研究了javascript / react-native一段时间了,无法弄清楚该怎么做。

//file1 (ImageHolder.js)
import type {ImageSourcePropType } from "react- native/Libraries/Image?ImageSourcePropType";

export type ImageHolder = {
  id: string,
  actualimage: ImageSourcePropType,
};

//file2(App.js)

import React from 'react';
import { StyleSheet, Text, View, Image, Button } from 'react- native';
import { type ImageHolder } from './ImageHolder'

const imageholder2: ImageHolder[] = [
  {
    id: "1",
    actualimage: require("./images/image1.jpeg"),
  },
  {
    id: "2",
    actualimage: require("./images/image2.jpg"),
  },
  {
    id: "3",
    actualimage: require("./images/image3.jpg"),
  },
  {
    id: "4",
    actualimage: require("./images/image4.jpg"),
  },
];



export default function App() {
  return (
    <View>
      <View>
        <Image
        style = {{
          width: 300,
          height: 300,
        }}
        source = {require('./images/image1.jpeg')} />
      </View>
      <View>
      <Button title= "Press me"/>
      </View>
    </View>
  );
}

我只是希望能够在按下按钮时浏览图像,并以某种方式访问​​我正在使用的内容。

文科夫斯基

保持当前图像索引处于一定状态,并在按下按钮时增加ir。

import React, { useState } from 'react'

export default function App() {
  const [currentImageIndex, setCurrentImageIndex] = useState(0)

  return (
    <View>
      <View>
        {
            imageholder2[currentImageIndex] &&
              <Image
                key={imageholder2[currentImageIndex].id}
                style = {{
                  width: 300,
                  height: 300,
                }}
                source={imageholder2[currentImageIndex].actualimage} 
              />
        }
      </View>
      <View>
      <Button 
          title= "Press me" 
          onPress={() => setCurrentImageIndex(currentImageIndex == imageholder2.length - 1 ? 
              0 : 
              currentImageIndex + 1
          )}
      />
      </View>
    </View>
  );
}

另外,最好将图像保持在一个状态。

很抱歉没有给出打字稿答案。

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

如何在本机反应中显示一组图像?

无限循环一组图像

如何从一组复杂的对象中获取一组唯一的属性

在一组图像中搜索图像

如何在活动中循环一组视图?

如何加载存储在 zip 文件中的一组图像?

如何使用循环将数字添加到 r 中的一组表的名称

如何在单个帧中显示一组视频

如何显示一组C ++中的某个项目

如何在Qt中多次显示一组动画?

如何循环浏览一组图片

从数据库下载一组图像,并使用HTTPRequest和HTTPResponse将其显示在Picturebox C#中

如何均衡一组图像的强度?

如何创建一组相关的属性?

Titanium PushNotification如何一组显示?

仅在选择特定的一组单选按钮中的至少一个时显示图像

如何使用ChartJS获取一组Date对象并根据一天中的时间显示频率

如何使用keySet()检索HashMap中的一组键,对其进行循环并找到每个键的计数?

如何在Matlab中从一组图像制作gif图像?

使用HTML5“必需”属性来显示一组复选框?

如何在 Rails 中的一个产品(属性)中创建一组用户(室友)

如何将一组属性映射到在 recharts 中具有该属性的 Bar?

如何知道一个点是否在图像中的一组点之外?

如何使用 Formik 更改一组复选框的 UI 状态

使用循环使第一列成为一组表中的行名

循环一组宏

如何将嵌套循环用于一组中两个坐标的距离?

如何为在Django中创建的for循环中重复的一组html代码运行JavaScript代码?

如何在 R 中的一组不同的变量和模型上创建循环