React - load new image - error - part 2

In summary, in React, loading a new image can sometimes result in an error. This can occur for a variety of reasons, such as incorrect file paths or a failure to properly handle the image load event. To prevent these errors, it is important to ensure that all file paths are correct and that the image load event is properly handled. Additionally, using error handling techniques such as try-catch blocks can help to catch and handle any errors that may occur during the image loading process.
  • #1
mathmari
Gold Member
MHB
5,049
7
pbuk said:
However you can also do this, described at https://create-react-app.dev/docs/adding-images-fonts-and-files, which has advantages (notably if logo.png is small enough it will be inlined instead of referenced which makes your site load faster).
Code:
# Set up your files like this
assets/img/logo.png
src/ui-component/Logo.js
Now write your component as:
JavaScript:
// Not sure if you need this but it is in the boilerplate in the docs.
import React from 'react';
// We need a relative reference from the location of this file.
import logo from '../assets/img/logo.png';

function Header() {
  return <img src={logo} alt="Logo" />;
}

export default Header;

It works with this code! If I want to write a text next to the image do I use <div> ?

I mean :

Code:
    return <div><div><img src={logo} alt="Logo" width="40" /></div><div>Title</div></div>;

🤔
 
Technology news on Phys.org
  • #2
mathmari said:
If I want to write a text next to the image do I use <div> ?

Code:
    return <div><div><img src={logo} alt="Logo" width="40" /></div><div>Title</div></div>;
That should work yes.

The inner <div> around the <img> seems a bit redundant though, and as an alternative for the title you might also use <p>Title</p>. 🤔
 
  • Like
Likes mathmari
  • #3
mathmari said:
It works with this code! If I want to write a text next to the image do I use <div> ?
Laying out text and images depends on what CSS framework you are using (Bootstrap?) so you would be best to check out the documentation for that. If you do want to ask here you could start a new thread but I really think you would be better off learning some more of the basics first. You could try starting with the links in this blog post https://blog.logrocket.com/using-bootstrap-with-react-tutorial-with-examples/
 
  • Like
Likes mathmari
  • #4
I like Serena said:
That should work yes.

The inner <div> around the <img> seems a bit redundant though, and as an alternative for the title you might also use <p>Title</p>. 🤔

It doesn't work, the text appears under the image.

I changed it now to :

Code:
import React from 'react';
import logo from '../assets/img/logo.png';
const Logo = () => {
    return (
        <div>
            <img src={logo} alt="abc" width="30" />
            <h3>Titel</h3>
        </div>
    );
};
export default Logo;

but again the titel is under the image. What am I doing wrong? 🤔
 
  • #5
mathmari said:
It doesn't work, the text appears under the image.

but again the title is under the image. What am I doing wrong?
Where do you want the title? I assumed you wanted it below the image.

The tags <div>, <p>, and <h3> are all block-style tags, which means they always start on a new line and can take up all of the horizontal space.
If you want the title to be to the right, you can use <span> or no tag at all.
The behavior of any of those tags can also be overruled with CSS. You can then use for instance the style inline-block. 🤔
 
  • Like
Likes mathmari
  • #6
I like Serena said:
Where do you want the title? I assumed you wanted it below the image.

The tags <div>, <p>, and <h3> are all block-style tags, which means they always start on a new line and can take up all of the horizontal space.
If you want the title to be to the right, you can use <span> or no tag at all.
The behavior of any of those tags can also be overruled with CSS. You can then use for instance the style inline-block. 🤔

I wrote now:

Code:
const Logo = () => {
    return (
        <div lass="box">
            <img src={logo} alt="logo" width="35" vertical-align="middle" />
            <span>TItel</span>
        </div>
    );
};

The titel is now next to the image, but is it possible that the Titel is next to the center of the image and not at the bottom? 🤔
 
  • #7
mathmari said:
I wrote now:

Code:
const Logo = () => {
    return (
        <div lass="box">
            <img src={logo} alt="logo" width="35" vertical-align="middle" />
            <span>TItel</span>
        </div>
    );
};

The titel is now next to the image, but is it possible that the Titel is next to the center of the image and not at the bottom?
Then the title must be in a container with the same height as the image, and it must be vertically aligned to the middle.
We can achieve that with for instance <table><tr><td><img src={logo}></td><td>Title</td></tr></table>. 🤔
 
  • Like
Likes mathmari
  • #8
I like Serena said:
We can achieve that with for instance <table><tr><td><img src={logo}></td><td>Title</td></tr></table>. 🤔
No, please not a table. Tables are used for tabulating things, not laying them out.

Writing HTML like this for React would be like writing cuneiform on a word processor.
 
  • Like
Likes mathmari
  • #9
I like Serena said:
Then the title must be in a container with the same height as the image, and it must be vertically aligned to the middle.
We can achieve that with for instance <table><tr><td><img src={logo}></td><td>Title</td></tr></table>. 🤔

It works in that way! :smile:
 
  • Like
Likes I like Serena
  • #10
pbuk said:
No, please not a table. Tables are used for tabulating things, not laying them out.

Writing HTML like this for React would be like writing cuneiform on a word processor.

So is it wrong to use tables there? 🤔
 
  • #11
Yes it is wrong.

HTML 5 specification (current) said:
Tables must not be used as layout aids. Historically, some Web authors have misused tables in HTML as a way to control their page layout. This usage is non-conforming, because tools attempting to extract tabular data from such documents would obtain very confusing results. In particular, users of accessibility tools like screen readers are likely to find it very difficult to navigate pages with tables used for layout.

HTML 4.01 specification (1999) said:
Tables should not be used purely as a means to layout document content as this may present problems when rendering to non-visual media. Additionally, when used with graphics, these tables may force users to scroll horizontally to view a table designed on a system with a larger display. To minimize these problems, authors should use style sheets to control layout rather than tables.
 
  • Informative
  • Like
Likes I like Serena and mathmari
  • #12
pbuk said:
Yes it is wrong.

So what do you suggest using instead? 🤔
 
  • #13
mathmari said:
So what do you suggest using instead? 🤔
You could try something like this:



Note that the "guts" of this is the HTML shown below. You will need to include the CSS for Bootstrap 5 using instructions like the ones I linked to earlier: https://blog.logrocket.com/using-bo...ples/#add-bootstrap-react-using-bootstrap-cdn.

HTML:
<div class="d-flex my-4">
  <div class="flex-shrink-0">
    <img src="https://via.placeholder.com/100" alt="An example image">
  </div>
  <div class="flex-grow-1 ms-3">
    <h2 class="h5">Top aligned by default</h2>
    Smaller images can keep the description on the same row even on mobile.
  </div>
</div>

<div class="d-sm-flex my-4 text-center text-sm-start align-items-center">
  <div class="flex-shrink-0">
    <img src="https://via.placeholder.com/320x180" alt="An example image">
  </div>
  <div class="flex-grow-1 ms-sm-3 mt-3 mt-sm-0">
    <h2 class="h5 d-none d-sm-block">Centered using
      <code>align-items-center</code></h2>
    Wider images should have a responsive layout so they move the description
    underneath on mobile.
  </div>
</div>
 
Last edited:
  • Like
Likes mathmari
  • #14
As an alternative, we can do:
HTML:
<div><img src={logo} style="vertical-align: middle;">Title</div>
The key is that the image itself must have a vertical alignment style. The text follows suit.
 
Last edited:
  • Like
Likes mathmari
  • #15
pbuk said:
You could try something like this:



Note that the "guts" of this is the HTML shown below. You will need to include the CSS for Bootstrap 5 using instructions like the ones I linked to earlier: https://blog.logrocket.com/using-bo...ples/#add-bootstrap-react-using-bootstrap-cdn.

HTML:
<div class="d-flex my-4">
  <div class="flex-shrink-0">
    <img src="https://via.placeholder.com/100" alt="An example image">
  </div>
  <div class="flex-grow-1 ms-3">
    <h2 class="h5">Top aligned by default</h2>
    Smaller images can keep the description on the same row even on mobile.
  </div>
</div>

<div class="d-sm-flex my-4 text-center text-sm-start align-items-center">
  <div class="flex-shrink-0">
    <img src="https://via.placeholder.com/320x180" alt="An example image">
  </div>
  <div class="flex-grow-1 ms-sm-3 mt-3 mt-sm-0">
    <h2 class="h5 d-none d-sm-block">Centered using
      <code>align-items-center</code></h2>
    Wider images should have a responsive layout so they move the description
    underneath on mobile.
  </div>
</div>


Using this code the title appears under the image. But why? 🤔
 
  • #16
I like Serena said:
As an alternative, we can do:
HTML:
<div><img src={logo} style="vertical-align: middle;">Title</div>
The key is that the image itself must have a vertical alignment style. The text follows suit.

Using this code the broswer shows me just a white page. But why? 🤔
 
  • #17
  • Like
Likes mathmari
  • #18
mathmari said:
Using this code the broswer shows me just a white page. But why? 🤔
No idea. Clearly you have done something basic wrong, at a guess you have not closed a tag properly. Is there a message shown in the browser console?

I'm beginning to think that you are "in over your head" with this, you need to go back to the basics and do a course learning HTML and CSS before you start doing anything in React. What are you trying to achieve anyway: do you even need React?
 
  • #19
mathmari said:
Using this code the broswer shows me just a white page. But why?
Right-click and Inspect? 🤔
 
  • Like
Likes mathmari
  • #20
pbuk said:
I'm beginning to think that you are "in over your head" with this, you need to go back to the basics and do a course learning HTML and CSS before you start doing anything in React. What are you trying to achieve anyway: do you even need React?
I believe the "point" of React is that it is easy to quickly create a website.
Of course React also has more advanced concepts, but it is not required to use them.
As for the CSS, it is not trivial to get a title vertically aligned to the right of an image, simple as that may seem at first.
 
  • #21
I like Serena said:
As an alternative, we can do:
HTML:
<div><img src={logo} style="vertical-align: middle;">Title</div>
The key is that the image itself must have a vertical alignment style. The text follows suit.

When I delete the part style="vertical-align: middle;" it works properly. 🤔
 
  • #22
mathmari said:
When I delete the part style="vertical-align: middle;" it works properly.
Perhaps a closing quote or an angled bracket was missing?
We can check if we inspect the generated html. 🤔
 
  • Like
Likes mathmari
  • #23
I like Serena said:
As an alternative, we can do:
HTML:
<div><img src={logo} style="vertical-align: middle;">Title</div>
The key is that the image itself must have a vertical alignment style. The text follows suit.
The style must be in {{.}}, i.e.

style={{ verticalAlign: 'middle' }}

Now it works! :smile:
 
  • Like
Likes I like Serena
  • #24
I like Serena said:
I believe the "point" of React is that it is easy to quickly create a website.
Of course React also has more advanced concepts, but it is not required to use them.
The point of React is that it is easy to quickly create a web application, or at least a website with dynamic content.

But before we can create a website with dynamic content we need to know how to create a website with static content.

And if we don't need dynamic content (there is nothing dynamic in what we have seen so far) then we don't need a front end framework.

I like Serena said:
As for the CSS, it is not trivial to get a title vertically aligned to the right of an image, simple as that may seem at first.
Indeed. This is why I recommend a CSS framework.
 
  • #25
Is there a way to make a word more bolder than " fontWeight: 'bold' " does? 🤔
 
  • #27
  • #28
  • Like
Likes mathmari
  • #29
pbuk said:
Your browser needs to have access to a font with an appropriate weight. This is often handled by the UI framework.

I haven't really understood what you mean. Could you explain that further to me? 🤔
 
  • #30
mathmari said:
I haven't really understood what you mean. Could you explain that further to me? 🤔
Sorry, I hit post before I had added a link, look again.
 
  • Like
Likes mathmari
  • #31
pbuk said:
Sorry, I hit post before I had added a link, look again.

Ah ok! 900 is the top, right? There is no way to make it bolder, is there? 🤔
 
  • #33
Great! Thank you! :smile:
 

FAQ: React - load new image - error - part 2

What does the error "React - load new image - error - part 2" mean?

The error "React - load new image - error - part 2" is a common error that occurs when trying to load a new image in a React application. It typically means that there is an issue with the code or the image itself, preventing it from being loaded correctly.

How can I fix the "React - load new image - error - part 2" error?

To fix this error, you will need to first check your code to ensure that the image path is correct and that the code is properly referencing the image. You should also check that the image file is in the correct format and is not corrupted. If the issue persists, you may need to seek further assistance or try a different method of loading the image.

Why is the "React - load new image - error - part 2" error occurring?

The "React - load new image - error - part 2" error can occur for a variety of reasons. Some common causes include incorrect code, incorrect file path, corrupted image file, or issues with the network connection. It is important to troubleshoot and identify the specific cause in order to effectively fix the error.

Can I prevent the "React - load new image - error - part 2" error from happening?

While it is not always possible to prevent this error from occurring, there are some steps you can take to reduce the likelihood of encountering it. These include double-checking your code for any errors, ensuring that the image file is in the correct format and not corrupted, and using a reliable network connection.

Are there any alternative solutions to fixing the "React - load new image - error - part 2" error?

If the usual methods of fixing the "React - load new image - error - part 2" error do not work, there may be alternative solutions that you can try. These include using a different method of loading the image, such as using a different library or component, or seeking assistance from other developers who may have encountered a similar issue.

Similar threads

Back
Top