0

I’ve just started using react native and expo and I’ve been restructuring folders over and over again because I run into multiple errors where a js file can’t find an image that’s located under ./asses/navigation/the_image.png. The only time I got it to work was having all the js files within the App.js and have the App.js located under “./” and the pictures in the same location.

This is what the error looks like: error

And this is what my project structure looks like: project structure

What’s the proper way to structure my project and how can I get files such as my profile.js to find images in assets?

Code for messages.js:

import 'react-native-gesture-handler';
import React, { useState } from 'react';
import { Image, StyleSheet, TouchableOpacity, View, TextInput } from 'react-native';

export default function messages({ navigation })
{
  const [filtertext, setfilter] = useState('');
  return(
    <View style={styles.home_container}>
      <View style={styles.home_background}>
        <TextInput
          style= {styles.filter} 
          placeholder= "Search"
          placeholderTextColor= "#96a7af"
          onChangeText={filtertext => setfilter(filtertext)}
          defaultValue={filtertext}
        />

        <Image source= {require ('./assets/navigation/3_Elements_Circled_Navigation_Message_On.png')}/> 

        <TouchableOpacity onPress={() =>navigation.navigate('Home')} style={styles.home_button}>        
          <Image source= {require ('./assets/buttons/new_message_icon.png')} style={styles.new_msg_buton}/> 
        </TouchableOpacity>

        <TouchableOpacity onPress={() =>navigation.navigate('Profile')} style={styles.profile_button}>        
          <Image source= {require ('./assets/buttons/profile_icon.png')}/> 
        </TouchableOpacity>

      </View>
    </View>
  );
}

const styles = StyleSheet.create(
{
  new_msg_buton:
  {
    position:'absolute',
    height: 60,
    width: 60,
    top: -696,
    left: 129,
  },

  filter:
  {
    position: 'absolute',
    height: 54,
    width: 275,
    top: -660,
    left: 19,
    paddingLeft: 20,
    borderColor: 'black',
    borderRadius: 23,
    borderTopWidth: 3,
    borderBottomWidth: 3,
    borderLeftWidth: 3,
    borderRightWidth: 3,
  }
})
4
  • Rather than posting the images on another site, paste the code directly here. Not everyone will click a link leading outside their current tab. reviewing and making corrections is also easier that way Commented May 20, 2020 at 0:15
  • you can also paste the images directly here Commented May 20, 2020 at 0:34
  • import image from '../assets/navigation/change_to_image_name.png'; // should works Commented May 20, 2020 at 1:00
  • I included the code in my post. Commented May 20, 2020 at 1:56

1 Answer 1

1

Using ./ refers to the current directory the file is located in

Using ../dir_onelevel_above refers to the dir_onelevel_above as you've gone one level above in the folder structure.

That being said, your should need ../assets/navigation/icon.png to access the image from messages.js

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

1 Comment

if this answer works for you, click the tick ✅ to help others find it

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.