Importing images in React
In this guide, weโll learn how to import images in React. Weโll cover:
- Importing images from project folders
- Using images in components
Importing images from project folders
Section titled โImporting images from project foldersโTo import images from your project folders, you can use the import
statement in JavaScript, just like we have done previously for modules and functions.
As with modules, you need to specify the path to the image file. You can call the image anything that makes sense to you.
Moving your image to the project folder
Section titled โMoving your image to the project folderโFirstly, you will need to move your logo to the app/assets/png
folder. This is where we will store all PNG images in the Epic News project.
The easiest way to do this is to simply drag and drop the image file into the app/assets/png
folder in your code editor:
Importing the image
Section titled โImporting the imageโNow that the image is in the correct folder, you can import it into your component:
import { useMatches } from '@remix-run/react'import { SearchBar } from '../molecules/SearchBar'import LoginOrUserDropdown from './LoginOrUserDropdown'import logo from '~/assets/png/epic-news-logo.png';
Using the image
Section titled โUsing the imageโTo actually use the image in your component, you can use the img
tag and set the src
attribute to the imported image.
You should also set an alt
attribute to describe the image for users accessing the site with a screen reader.
Add the line of code below inside the div
that you created in the previous tutorial:
4 collapsed lines
import { useMatches } from '@remix-run/react'import { SearchBar } from '../molecules/SearchBar'import LoginOrUserDropdown from './LoginOrUserDropdown'import logo from '~/assets/png/epic-news-logo.png'
export default function HeaderWithSearch() { const matches = useMatches() const isOnSearchPage = matches.find(m => m.id === 'routes/users+/index') const searchBar = isOnSearchPage ? null : <SearchBar status="idle" />
return ( <header className="dark:bg-dark-primary/10 bg-primary/10 py-6"> <nav className="container flex flex-wrap items-center justify-between gap-4 sm:flex-nowrap md:gap-8"> <div> <img src={logo} alt="Epic News Logo" className="w-16" /> <span className="text-sm text-foreground">Epic News</span> </div>7 collapsed lines
<div className="ml-auto hidden max-w-sm flex-1 sm:block"> {searchBar} </div> <div className="flex items-center gap-10"> <LoginOrUserDropdown /> </div> <div className="block w-full sm:hidden">{searchBar}</div> </nav> </header> )}
With this code saved, your logo should now appear just above the site title in the navigation bar:
Summary
Section titled โSummaryโIn this tutorial, we have:
- Moved an image to the
app/assets/png
folder - Imported the image into a component
- Used the image as the
src
attribute in animg
tag - Positioned the image and text in the navigation bar correctly using flexbox
Whatโs next?
Section titled โWhatโs next?โIn the next tutorial, weโll customise the navigation bar and footer by adding links to different pages in the site.