[英]How to loop in MasonryList on React Native?
I want to use MasonryList component in my react native project.我想在我的本机项目中使用 MasonryList 组件。
So i created a component like this below:所以我创建了一个如下所示的组件:
import React, {Component} from 'react';
import {Image, StyleSheet, View, TouchableOpacity, Alert} from 'react-native';
import MasonryList from 'react-native-masonry-list';
import {books} from '../assets';
class Books extends Component {
render() {
return (
<View style={styles.container}>
<MasonryList source={[{
uri: '...'
}]} />
</View>
);
}
}
so, "uri" prop expects a link for a single image, if you want to use multiple images you should have a structure like this.所以,“uri”道具需要一个单一图像的链接,如果你想使用多个图像,你应该有这样的结构。
{ uri: "link" }, {uri: "link2"}, {uri: "link3" }
and so on...等等...
I have imported the books, so my question is how can i loop in the components itself, to reach each element's "thumbnail" to get the url on the structure below?我已经导入了书籍,所以我的问题是如何在组件本身中循环,以到达每个元素的“缩略图”以获取下面结构上的 url?
my books.js file structure is like:我的 books.js 文件结构是这样的:
export default {
items: [
{
kind: "books#volume",
id: "md5"
volumeInfo: {
imageLinks: {
thumbnail: "
Simply i could do mapping in the render function and create MasonryList component but that would create the component as much as the loop counts.简单地说,我可以在渲染函数中进行映射并创建 MasonryList 组件,但这将创建与循环计数一样多的组件。 I want only 1 component and multiple uri links in it.我只想要 1 个组件和多个 uri 链接。
You can use a state array in "source" prop and in componentDidMount or at any relevant location, set this state using map function.您可以在“source” prop 和 componentDidMount 或任何相关位置使用状态数组,使用 map 函数设置此状态。
import React, {Component} from 'react';
import {Image, StyleSheet, View, TouchableOpacity, Alert} from 'react-native';
import MasonryList from 'react-native-masonry-list';
import {books} from '../assets';
class Books extends Component {
constructor(props){
super(props);
this.state={
imageArray: [],
}
}
componentDidMount(){
var imageArray = [];
books.map(book =>{
imageArray.push({uri: book.volumeInfo.imageLinks.thumbnail})
});
this.setState({imageArray});
}
render() {
const {imageArray} = this.state;
return (
<View style={styles.container}>
<MasonryList source={imageArray} />
</View>
);
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.