react-native - react-native-image-slider-box 与本地图像不起作用
问题描述
我正在尝试使用“react-native-image-slider-box”库创建一个图像轮播。我正确配置了库类和参数,但无法在移动博览会应用程序中看到图像轮播。
我使用的代码:
import React, { Component } from 'react';
import { StyleSheet, View } from 'react-native';
import { SliderBox } from 'react-native-image-slider-box';
import { HomeGrid } from './HomeGrid';
export default class Home extends Component {
constructor() {
super();
this.state = {
images: [
require('./commonComponents/photos/photo2.jpeg'),
require('./commonComponents/photos/photo3.jpg'),
require('./commonComponents/photos/photo4.jpg'),
require('./commonComponents/photos/photo3.jpg'),
]
};
}
render() {
return (
<View style={styles.container}>
<SliderBox
images= { {uri: this.state.images } }
sliderBoxHeight={400}
onCurrentImagePressed={index =>
console.warn(`image ${index} pressed`)
}
/>
<HomeGrid />
</View>
);
}
}
const styles = StyleSheet.create({
container: {
flex: 1
}
});
并且图像存在于这样的目录中:
ProjectName
---> src
---> components
---> commonComponents
---> photos
---> photo1.jpeg
我在哪里错了?我应该怎么做才能使图像滑块工作?输出仅显示 HomeGrid 组件而不是 SliderBox 组件。
解决方案
在下面的react-native-image-slider-box示例中,它只是像this.state.images
without一样导入图像uri
:
<SliderBox
images={this.state.images} // like this
onCurrentImagePressed={index => console.warn(`image ${index} pressed`)}
currentImageEmitter={index => console.warn(`current pos is: ${index}`)}
/>
------更新尝试代码------------工作正常----
import React, { Component } from "react";
import { StyleSheet, View } from "react-native";
import { SliderBox } from "react-native-image-slider-box";
export default class Home extends Component {
constructor(props) {
super(props);
this.state = {
images: [
"https://images1.epochhk.com/pictures/112999/Fotolia_61981708_Subscription_L@1200x1200.jpg",
"https://i1.read01.com/SIG=3d308k4/304a3259317255786a6f.jpg",
"https://images1.epochhk.com/pictures/112999/Fotolia_61981708_Subscription_L@1200x1200.jpg",
"https://i1.read01.com/SIG=3d308k4/304a3259317255786a6f.jpg"
]
};
}
render() {
return (
<View style={styles.container}>
<SliderBox
images={this.state.images}
sliderBoxHeight={200}
onCurrentImagePressed={index =>
console.warn(`image ${index} pressed`)
}
/>
</View>
);
}
}
const styles = StyleSheet.create({
container: {
flex: 1
}
});
推荐阅读
- twilio - 我从 Twilio 神秘地收到 11200 错误(403 Forbidden on callback)
- r - 创建一个显示按年份发布的项目数量的图
- sql - 获取不同列中重复值的数量
- java - 我完全不知道如何开始,更不用说完成学校的编码项目了
- excel - 仅使用 VBA 将数据从 Excel 365 上传到 Azure Sql Server
- sql - 循环内的 SELECT 语句
- lua - 是否有针对本地用户的 Nil 值的修复
- laravel - 如何使用 laravel mix 和 webpack 将环境变量传递给 scss/sass 文件?
- javascript - CSS用字母填充div的整个宽度,而不改变字间距
- python-3.x - 如何使用 Selenium 和 BeautifulSoup 抓取页面然后执行按钮单击以转到下一页进行抓取