0

Im trying to get a more dynamic way of showing articles. I made a JSON file which contains all the info and loop over it and for each item it creates an item on the homepage. This is working and the title and text ar shown. Now im having trouble with the images. I dont get any errors so i think i might have to do with the path im calling in the json file.

project structure:

src
--articles
----img
------img.jpg
----articlePreview.js
----articles.json
----fullArticle.js
--homepage
----index.js 
App.js

Testing json, as you can see i tried some different paths and tried some more as well:

{
    "articles": [
        {
            "title": "Test",
            "imageName": "/img/img.jpg",
            "text": "more text to test"
        },
        {
            "title": "Test22",
            "imageName": "./img/img.jpg",
            "text": "more text to test"
        }
    ]
}

On the homepage i map all the items in the json file like this:

  const contents = json.articles.map(function (item) {
    return (
      <ArticleHome title={item.title} text={item.text} image={item.imageName} />
    );
 });

and finally the component which should display them:

function ArticleHome({title, text, imageName}) {

  return (
    <View>
      <Card>
        <CardItem header bordered>
          <Body>
            <Image
              style={{ width: '100%', height: 400 }}
              source={{uri: imageName}} 
            />
            <Text>
              {title}
            </Text>
            <Text>
              {text.substring(0,240)}...
            </Text>
            <Button
              title="Lees Meer"
              color="#d10a10"
              onPress={() => RootNavigation.navigate('Article')}
            />
          </Body>
        </CardItem>

      </Card>
    </View>
  );
}

2 Answers 2

0

Instead of passing image in this way

"imageName": "/img/img.jpg",

You have to get an image like below.

imageName: require("./img/img.jpg"),

And the simply pass this path to your Image tag

<Image source={imageName} />

Here is a demo snack.

https://snack.expo.io/@waheed25/smiling-carrot

Edit: Here is a demo with your code

https://snack.expo.io/@waheed25/c91df6

Sign up to request clarification or add additional context in comments.

9 Comments

that doesnt seem to work, getting unknown know module "./img/img/jpg"
Are you passing exactly the same? "./img/img/jpg"? If yes then pass proper name of the image with extension.
i made a typo but yes im passing ./img/64332.jpg which is in the img directory
Remove double quotes from your "imageName" in your JSON file.
I also added a demo to my answer.
|
0

The only thing that worked for me was using encode64 image:

{ data.json = 
    "1":{
        "id":"1",
        "text": "test",
        "img": "encode64img" // something like data:image/png;base64,iVBORw0KGgoAAAA
    }
}

Then in your app:

    import data from './data.json'
 const[uri, setUri] = useState(data[1].img)
 <Image style={styles.img} source={{uri: uri}}/>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.