1

Hey guys I'm making a TodoApp in react js, and I have a simple layout and I wanted to add a few images to the app. But when I add the images it is not loading that image.

Here is the code

import TodoList from './components/TodoList';
import imagem from './skate.png'


function App() {
  return (
    
    <div className="todo-app">
        <TodoList />
        <img src="imagem"/>
    </div>
  );
}

export default App;

And here is an image of what´s happening:

6 Answers 6

4

When using an bundler (like Parcel or Webpack) to handle images, you need to remember that the imported value is a variable.

You pass variables to props with { and }. Using a pair of " gives you a hardcoded string.

    <img src={imagem} />
Sign up to request clarification or add additional context in comments.

3 Comments

Ty so much. So if i used a website link i used "" instead {} is that?
If you wanted to pass a hard coded string with a URL in it then you would use " and ".
hmm okok make sense
2

So, In React.js (jsx) things don't work quite like HTML. WHat you are doing is importing the image from its location, by using: import image from './skate.png';

and then, inside of your function. If you import anything (image, css file, json file), for you to be able to use it you need to call it.

for example:

import JsonData from './data.json'

function App(){
return(
  <div>
    <JsonData/>
  </div>
)

}

To call it. SO in you case, you just need to replace to

and it should work.

1 Comment

No, the imported image is not a component, and not would imported JSON.
2

How about if you do this:

<img src={imagem} />

otherwise, it gets interpreted as a string and you have no http://../imagen on your site.

2 Comments

oh ok makes sense Ty so much, I'm new at this
Those things get more obvious as you do more react. Glad to help.
2

The problem with your code is that

"imagem"

is read as a String. To read it as an image, place {} around it as so:

{imagem}

3 Comments

oh ok! make sense ty
No problem! I love the design of your website :)
ahahah ty so much <3
1

"imagem" is string. Need variable {imagem].

1 Comment

oh ok make sense, ty I'm new at this <3
1

when you use qoutes "" in jsx, it consumes it as a string and when you use {}, it consumes it as normal JS, so in your case, if you use

<img src={imagem} />

that would solve your problem

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.