简体   繁体   中英

React Native App content now showing on the emulator

I am working on react native that gets content from data file to be displayed as new items containing, image, title, and content, I created a component named Post with image, title, and content as props but when I try to display on FlatList the news items do not show on the emulator. I do not know what I am getting wrong and I would appreciate getting some guidance. Below are the files and structure of my implementation. The problem is that I am getting a blank screen on the emulator window!!

Data.json

{

    "posts": [
        {
          "id" : 1,
          "title": "The Best Article Ever Written",
          "img": "https://i.imgur.com/mf9daCT.jpg",
          "content": "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum",
          "author": "Bob Labla"
        },
        {
          "id" : 2,
          "title": "Making the Best of Things",
          "img": "https://i.imgur.com/lxs9pM1.jpg",
          "content": "Sed ut perspiciatis unde omnis iste natus error sit voluptatem accusantium doloremque laudantium, totam rem aperiam, eaque ipsa quae ab illo inventore veritatis et quasi architecto beatae vitae dicta sunt explicabo. Nemo enim ipsam voluptatem quia voluptas sit aspernatur aut odit aut fugit, sed quia consequuntur magni dolores eos qui ratione voluptatem sequi nesciunt. Neque porro quisquam est, qui dolorem ipsum quia dolor sit amet, consectetur.",
          "author": "Joe Shmo"
        },
        {
          "id" : 3,
          "title": "Why I'm Right and You're Wrong",
          "img": "https://i.imgur.com/UfXZnp4.jpg",
          "content": "At vero eos et accusamus et iusto odio dignissimos ducimus qui blanditiis praesentium voluptatum deleniti atque corrupti quos dolores et quas molestias excepturi sint occaecati cupiditate non provident, similique sunt in culpa qui officia deserunt mollitia animi, id est laborum et dolorum fuga.",
          "author": "Mister Magoo"
        }
    ]
}

Post has index.js and Style.js

index.js

import React from 'react';
import { Image, Text, TouchableOpacity, View} from 'react-native';

import styles from './styles';

const Post = ({ img, title, content }) => (
  <View style={styles.main}>
    <Image
      source={{ uri: img }}
      style={styles.image}
    />
    <View style={styles.content}>
      <Text style={styles.title}>{title}</Text>
      <Text>{content}</Text>
    </View>
    <TouchableOpacity style={styles.button} activeOpacity={0.8}>
      <Text style={styles.buttonText}>Read more</Text>
    </TouchableOpacity>
  </View>
);

export default Post;

styles.js

import { StyleSheet } from 'react-native';

const styles = StyleSheet.create({
  main: {
    backgroundColor: '#fff',
    borderRadius: 3,
    height: 340,
    margin: 5,
    width: 240,
  },
  image: {
    backgroundColor: '#ccc',
    height: 120,
    resizeMode: 'cover',
  },
  content: {
    padding: 10,
    overflow: 'hidden',
    zIndex: 1,
    flex: 1,
  },
  title: {
    fontSize: 18,
    marginBottom: 5,
  },
  button: {
    backgroundColor: '#1abc9c',
    borderRadius: 3,
    padding: 10,
    margin: 10,
  },
  buttonText: {
    color: '#fff',
    textAlign: 'center',
  },
});

export default styles;

Apps.js calls all the other files

//import { StatusBar } from 'expo-status-bar';
import React,{ Component } from 'react';
import { StyleSheet, Text, View, FlatList, SafeAreaView, StatusBar } from 'react-native';

import data from './data.json';
import Post from './Post';


export default function App (props) {
  
  console.log(data.posts[0])
  const renderItem = ({ item }) => {
    return (
      <View style={styles.container}>
         <View style={styles.toolbar}>
           <Text style={styles.title}>Latest posts</Text>
        </View> 
    
          {/* <Post {...post} />  */}
         <Post  img={item.img} title={item.title} content={item.content} />   
      </View>
    );
  }

  return (
    <SafeAreaView style={styles.container}>
       <FlatList
          data={data.posts}
          keyExtractor={item => item.id}
          renderItem={renderItem}
       />
    </SafeAreaView>
  );
}

const styles = StyleSheet.create({
  container: {
    flex: 1,
    marginTop: StatusBar.currentHeight || 0
  },
  toolbar: {
    backgroundColor: '#34495e',
    padding: 10,
    paddingTop: 20,
  },
  title: {
    color: '#fff',
    fontSize: 20,
    textAlign: 'center',
  },
  list: {
    backgroundColor: '#f0f3f4',
    paddingTop: 5,
    paddingBottom: 5,
    flex: 1
  },
  content: {
    flexDirection: 'row',
    flexWrap: 'wrap',
    justifyContent: 'center',
  },
})

In your data.json add require() near all image like this:

"img": require("https://i.imgur.com/mf9daCT.jpg")

And add width to post image styles

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM