【问题标题】:React Native, Adding Image Beside FlatList itemReact Native,在 FlatList 项目旁边添加图像
【发布时间】:2021-03-01 09:55:14
【问题描述】:

这是使用 React Native 构建的简单代码,它运行良好,它从 JSON 链接获取数据并将其作为文本项显示在 FlatList 中。我想在这个 FlatList 中添加图像 foreach 项目。我不知道这段代码的哪一部分会显示图像。 这是 JSON 文件:

[
  {"uid":"001","uname":"Jon","uimage":"http:\/\/me.com\/jon.jpg"},
  {"uid":"002","uname":"Eds","uimage":"http:\/\/me.com\/eds.jpg"},
  {"uid":"003","uname":"Sam","uimage":"http:\/\/me.com\/sam.jpg"}
]

这是 RN 代码:

import React, { useState, useEffect } from 'react';
// import all the components we are going to use
import {
SafeAreaView,
Text,
StyleSheet,
View,
FlatList,
Image
} from 'react-native';
const App = () => {
const [masterDataSource, setMasterDataSource] = useState([]);

useEffect(() => {
    fetch('http://www.link2json.com/data.json')
        .then((response) => response.json())
        .then((responseJson) => {
            setMasterDataSource(responseJson);
        })
        .catch((error) => {
            console.error(error);
        });
}, []);

const ItemView = ({ item }) => {
    return (
        // Flat List Item
        <Text
            style={styles.itemStyle}
            onPress={() => getItem(item)}>
            {item.unam}
        </Text>           
        <Image source={ uri: {'uimage'}} /> //<--Here's the issue
    );
};

const ItemSeparatorView = () => {
    return (
        // Flat List Item Separator
        <View
            style={{
                height: 0.5,
                width: '100%',
                backgroundColor: '#C8C8C8',
            }}
        />
    );
};

const getItem = (item) => {
    // Function for click on an item
    alert('Id : ' + item.uid + ' Title : ' + item.unam);

};

return (
    <SafeAreaView style={{ flex: 1 }}>
        <View style={styles.container}>
            <FlatList
                data={masterDataSource}
                keyExtractor={(item, index) => index.toString()}
                ItemSeparatorComponent={ItemSeparatorView}
                renderItem={ItemView}
            />
        </View>
    </SafeAreaView>
);
};

const styles = StyleSheet.create({
container: {
    backgroundColor: 'white',
},
itemStyle: {
    padding: 10,
},
});

export default App;

【问题讨论】:

    标签: javascript json react-native url


    【解决方案1】:

    我同意最后一个答案,但很少做造型。实际上您不能返回 2 个视图项,因此您需要添加一个父视图以使用其中的其他项,这样您只返回单个视图项。

    const ItemView = ({ item }) => {
        return (
           <View style={{flexDirection:'row', justifyContent: 'space-between'}}>
            <Text
                style={styles.itemStyle}
                onPress={() => getItem(item)}>
                {item.unam}
            </Text>           
            <Image style={{height:50,width:50}} source={{uri: item.uimage}} />
           </View>
        );
    };
    

    【讨论】:

    • 没有更多错误,但项目旁边没有图像。
    • 你能在你的问题中添加一个原型图片吗?
    【解决方案2】:

    试试这个

    <Image style={{height:50,width:50}} source={{uri: item.uimage}} /> // add here
    

    【讨论】:

      【解决方案3】:

      您必须执行以下操作

      const ItemView = ({ item }) => {
          return (
              // Flat List Item
             <View style={{flexDirection:'row'}}>
              <Text
                  style={styles.itemStyle}
                  onPress={() => getItem(item)}>
                  {item.unam}
              </Text>           
              <Image style={{height:50,width:50}} source={{uri: item.uimage}} />
             </View>
          );
      };
      

      【讨论】:

      • 您建议的代码不会引发错误,但不会在 Flatlist 项目旁边显示图像。
      • @HowardJohn 你能在浏览器中访问 uimage url 并查看图像吗?
      • 是的,即使我使用此图片链接来确保:reactnative.dev/img/tiny_logo.png
      • 我测试过,效果很好,你添加了上面的样式对吗?
      • @HowardJohn 检查这个零食snack.expo.io/8rjt_7MS7
      猜你喜欢
      • 1970-01-01
      • 2020-07-18
      • 2020-07-27
      • 2021-07-25
      • 2021-08-22
      • 1970-01-01
      • 2021-05-17
      • 2019-03-17
      • 1970-01-01
      相关资源
      最近更新 更多