Adding Images, Fonts, and Files | Create React App (2024)

With webpack, using static assets like images and fonts works similarly to CSS.

You can import a file right in a JavaScript module. This tells webpack to include that file in the bundle. Unlike CSS imports, importing a file gives you a string value. This value is the final path you can reference in your code, e.g. as the src attribute of an image or the href of a link to a PDF.

To reduce the number of requests to the server, importing images that are less than 10,000 bytes returns a data URI instead of a path. This applies to the following file extensions: bmp, gif, jpg, jpeg, and png. SVG files are excluded due to #1153. You can control the 10,000 byte threshold by setting the IMAGE_INLINE_SIZE_LIMIT environment variable as documented in our advanced configuration.

Here is an example:

import React from 'react';
import logo from './logo.png'; // Tell webpack this JS file uses this image

console.log(logo); // /logo.84287d09.png

function Header() {
// Import result is the URL of your image
return <img src={logo} alt="Logo" />;
}

export default Header;

This ensures that when the project is built, webpack will correctly move the images into the build folder, and provide us with correct paths.

This works in CSS too:

.Logo {
background-image: url(./logo.png);
}

webpack finds all relative module references in CSS (they start with ./) and replaces them with the final paths from the compiled bundle. If you make a typo or accidentally delete an important file, you will see a compilation error, like when you import a non-existent JavaScript module. The final filenames in the compiled bundle are generated by webpack from content hashes. If the file content changes in the future, webpack will give it a different name in production so you don’t need to worry about long-term caching of assets.

Please be advised that this is also a custom feature of webpack.

It is not required for React but many people enjoy it (and React Native uses a similar mechanism for images).

An alternative way of handling static assets is described in the next section.

Adding SVGs

Note: this feature is available with react-scripts@2.0.0 and higher, and react@16.3.0 and higher.

One way to add SVG files was described in the section above. You can also import SVGs directly as React components. You can use either of the two approaches. In your code it would look like this:

import { ReactComponent as Logo } from './logo.svg';

function App() {
return (
<div>
{/* Logo is an actual React component */}
<Logo />
</div>
);
}

This is handy if you don't want to load SVG as a separate file. Don't forget the curly braces in the import! The ReactComponent import name is significant and tells Create React App that you want a React component that renders an SVG, rather than its filename.

Tip: The imported SVG React Component accepts a title prop along with other props that a svg element accepts. Use this prop to add an accessible title to your svg component.

Adding Images, Fonts, and Files | Create React App (2024)

FAQs

How do you add images to create-react-app? ›

The most straightforward way to import an image in React is to import it directly into your component file. This method assumes that the image file is located within your project directory. Here's a code snippet that demonstrates this: import React from 'react'; import myImage from './path_to_your_image.

In which folder should static assets like images and fonts be placed in a React app? ›

In a React application, the src folder is where you put the source code for your application, including your JavaScript, CSS, and other assets. The public folder is where you put the files that need to be publicly available, such as images, fonts, and other static files.

How do I add fonts to a React application? ›

Manually add Google Fonts in React by including the link tag with the Google Fonts URL in the public/index. html file's <head> section, then apply the font using CSS in React components.

Where to store a logo in React? ›

The src folder is the main folder for your React application. This is where you will put all of your JavaScript files, as well as any other assets that you want to be bundled up with your application. You can also put images in the src folder.

How do I add an upload image to React? ›

Table of contents
  1. Setup a ReactJS app with Vite and TailwindCSS.
  2. Complete the uploader structure.
  3. Handling the file chooser.
  4. Customizing the uploader with reference(useRef)
  5. Handling the file change event.
  6. Previewing the uploaded image from the browser cache.
  7. Uploading the image to the server.
  8. A couple of tasks for you!
Mar 8, 2024

What is the best folder structure for react app? ›

Ideal Folder Structure
  • The structure is organized into an src the directory containing the React app's source code.
  • The actions and reducers folders contain Redux-specific code.
  • The components folder contains the React components for the app.
  • The styles folder contains CSS styles.
Feb 9, 2023

How do I store images in local storage in React? ›

Try adding "setUrl(localStorage. getItem('recent-image'));" to the logic in your event listener callback. React re-renders components when state is updated, so you need to change state to see the newly uploaded image.

What is the best styling library for React? ›

The 6 best React UI component libraries list includes: Material UI, React-Bootstrap, Chakra UI, Ant Design, Blueprint UI, and PrimeReact. Explore more details here! React JS dominates front-end frameworks, allowing developers to easily construct compelling and dynamic user experiences.

How do I import a font into React styled components? ›

import { css } from 'styled-components'; import { fontWeight } from './vars'; import myFontURL from '../../mypath/fonts/OurFont-Bold. woff2'; export const fontFaces = css` @font-face { font-family: 'OurFont'; src: url(${myFontURL}) format('woff2'); font-weight: ${fontWeight.

Where to keep images in React Project? ›

The first way to import images in React is by adding them to the source folder (testapp/src/) of your application. Thanks to webpack, it is possible to import a file right in a JavaScript module. By default, when you create a react app with create-react-app, React adds logo. svg in the src folder.

What is the default font in React? ›

By default, RN uses the default font family by the platforms; ie, 'Roboto' for android and 'san Francisco' for ios.

How do I add a PNG logo to React? ›

Here is an example:
  1. import React from 'react';
  2. import logo from './logo.png'; // Tell webpack this JS file uses this image.
  3. console. log(logo); // /logo.84287d09.png.
  4. function Header() {
  5. // Import result is the URL of your image.
  6. return <img src={logo} alt="Logo" />;
  7. }
  8. export default Header;
Feb 13, 2020

How to import jpg image in React? ›

You can follow these steps to import local images to a React component and loop through them:
  1. Create an images folder and put your images inside this folder.
  2. Import each image file into your component file using import statements. You can define each imported image as a variable.

How do I import all images from a folder in React? ›

context method.
  1. First, create a directory to store all your images. For example, let's create a folder named "images" in the src folder.
  2. Place all your images inside the "images" folder.
  3. In your React component file, import the require context method as shown below:

How do I add an external image to React? ›

To display a react image that is hosted on an external server in a React component, you can use the <img> HTML tag inside a component's render method. For example: This will render a react image with the specified src URL in your React component.

How do I add icons to react app? ›

[React] - How to import icons into React
  1. Find the specific icon you want to use on the icon library's website or documentation. Each icon should have a unique class name.
  2. Add the icon to your component using the library's icon component or HTML tag (depending on the library).

How do I host an image on react app? ›

To upload image and preview it using React JS we will use the HTML file input for the image input. After taking input the image url is created using URL.createObjectURL() method and store in the useState variable named file. Display the image as preview using the html img tags with the file url in the src prop.

Top Articles
Latest Posts
Article information

Author: Dong Thiel

Last Updated:

Views: 5961

Rating: 4.9 / 5 (59 voted)

Reviews: 82% of readers found this page helpful

Author information

Name: Dong Thiel

Birthday: 2001-07-14

Address: 2865 Kasha Unions, West Corrinne, AK 05708-1071

Phone: +3512198379449

Job: Design Planner

Hobby: Graffiti, Foreign language learning, Gambling, Metalworking, Rowing, Sculling, Sewing

Introduction: My name is Dong Thiel, I am a brainy, happy, tasty, lively, splendid, talented, cooperative person who loves writing and wants to share my knowledge and understanding with you.