Where should I store dynamically uploaded images in create-react-app?

Hey there! I've been using create-react-app and implemented a file upload feature for images. The uploads are then sent to the backend and saved locally within the project's build directory. Currently, I can reference these images dynamically via localhost:4000/image.png, which works well for me (e.g., uploading images for a blog and accessing them later in a blogList).

However, I have realized that this may not be the ideal way to handle image uploads. I've gone through the documentation provided by create-react-app, but the suggested methods for handling images don't seem to fit my requirements. I must be missing something crucial.

The docs mention: - Using "import," but since I'm loading images dynamically, I'm unsure of how this would apply? [Link to docs] - Using the public folder, but wouldn't that require a rebuild? As I am dynamically loading images, rebuilding is not feasible. [Link to docs]

Just to clarify, my current setup involves an image uploader and backend that functions smoothly with the /build directory. I can upload images and refer to them dynamically. I simply want to know the best practices for achieving this. Thank you! (If suggesting the use of Nginx, could you provide some insight into its implementation?)

Answer №1

When faced with a similar challenge as a learner, I encountered difficulties and experimented with various solutions some time ago. While some suggested using /public for seldom accessed assets could be helpful, my situation involved clients uploading and managing images within an app. Like yourself, I was dissatisfied with those suggestions and eventually devised this particular configuration.

  • Established a /public directory on the backend specifically for image uploads located at /public/images
  • Served the /public directory via Express to enable static file access
  • Implemented a dynamic path variable (configured accordingly) for image pathways, utilizing during development and /public/images in production.

While I cannot claim that this approach is optimal, it personally proved effective. Prior to implementing this setup, I encountered issues where CRA would refresh my application after each image upload, impacting the overall user experience.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Having trouble with a 404 error on form submission with React and Express?

I am currently working on a basic MERN application. My goal is to establish a connection between the front and back ends in order to post data on the server upon form submission. While I can access the server URL directly, I encounter a 404 error (Not Foun ...

How can I deploy React applications to AWS EC2 using Github Actions and AWS CodeDeploy?

I am currently facing a challenge in setting up a React app within Github Action workflow and deploying it on an EC2 instance. The issue I'm encountering is the inability to access the /build folder that is created during the action process. Below i ...

Is it normal for Tailwind animation to loop twice when transitioning between pages in Next.js?

I'm currently utilizing react-hot-toast for displaying alerts and animating them during page transitions. The animation involves a double fade-in effect when transitioning between pages. In my project, I've integrated tailwindcss-animate within ...

What is the reason behind one function triggering a re-render of a component while the other does not in Next.js?

I am currently working on a Next.js web application where one of the pages contains two functions that utilize useState() to add or remove emails from an array. const [invites, setInvites] = useState([]) // other code const lmao = () => { console.lo ...

Iterating over a nested document containing subarrays and rendering them on an EJS template

My aim here is to iterate through an embedded document and display comments for the posts with matching IDs. This is my Schema structure on the node.js server: var postSchema = new mongoose.Schema({ name: String, post: String, comment: [ ...

No need for jQuery with this scrolling image gallery

I recently created a horizontal scrolling image gallery with thumbnails underneath. The goal is to click on a thumbnail and have the corresponding image scroll into view. Here's the HTML setup: <div class="images_container"> <img id="imag ...

The values obtained from the previous parameter object of the React setState hook can vary and are not always

In my code, I am using a useEffect hook to update the state with setState. However, I'm encountering some unusual and inconsistent behavior with the previous parameter: useEffect(() => { setCurrentPicturesObject((existing) => { ...

Efficient React search feature with pagination that avoids redundant setState calls

Currently in the process of incorporating search functionality with pagination in React. After reviewing numerous examples, it appears they all involve using setState() twice, both before and after making an AJAX call to the backend. Here is a snippet of m ...

Attempting to send a POST request, only to be informed by the form that it is devoid of

I have been struggling with this problem for some time now. I implemented the category_create_post functionality in the categoryController, and everything seems to be set up correctly. I also configured the category_form.ejs to accept user input. However, ...

Seamless Authentication - automatic login following registration

Are there any resources available on automatically signing in a user after they have signed up? I could only come up with one potential solution: signing up the user first and then programmatically calling the signIn function provided by next-auth to log ...

Input of data and salt must be provided

(node:35) UnhandledPromiseRejectionWarning: Error: data and salt arguments required. Can someone please assist me in resolving this error that I am encountering? const validatePassword = (password) => { return password.length > 4; }; app.post("/r ...

Tips for minimizing the height of the Material Toolbar in Material-UI

I'm looking to customize the height of the toolbar in Material-UI to make it smaller Although I checked out How do I change the Material UI Toolbar height?, I am still struggling with this issue Increasing the height above 50 seems to work, but redu ...

I encountered an issue when trying to include the dotenv file, receiving the following error message: [TypeError: Network request failed]

babel.config.js File plugins: [ ["module:react-native-dotenv", { "envName": "APP_ENV", "moduleName": "@env", "path": ".env", "blocklist": null, "allowlist": null, "blacklist": null, // DEPRECATED "whitelist": ...

Notification within the conditional statement in React JS

I am working on validating phone number input within a React JS component using an if/else statement. If the user enters letters instead of numbers, I want to display a message saying "please check phone number". While I have been able to create a function ...

Guide on programmatically importing excel/CSV data into MongoDB collection/documents using MERN stack

I have a spreadsheet containing employee information. My objective is to save this data from the Excel file into a MongoDB database, specifically in the employee collection (each row from the spreadsheet as a document in MongoDB). This process is being car ...

Changing the background color of a selected table row in React using Material-UI

Utilizing React and Material-UI, I am facing an issue where the selected table row background inherits the muiTheme borderColor by default. However, changing the borderColor affects other elements as well. How can I adjust only the table row background f ...

"Error: It seems like the 'express' command is not recognized. Were you

I am currently working on an OSX 10.10.4 operating system and have previously used node.js and express on this machine. However, I am now facing a situation where the computer is not recognizing the command express when trying to start a new application. ...

Is Jade monitoring *.jade files?

Though I am not sure of the internal workings of Jade, my best guess is that it compiles each template file once and then employs a compiled and cached version for subsequent HTTP requests. One intriguing observation I have made while running my Express a ...

Ways to determine the count of selected checkboxes in React.js?

Recently, I delved into learning React. For one of my beginner projects, I decided to create a "life checklist" using Functional Components as the foundation. Just a heads up: I've structured data.js as an array of objects containing "action", "emoj ...

The JWT token will expire if the user logs in from a different browser

My application generates a JWT token for the user when they log in, but I am facing an issue. I want the user to be automatically logged out from their first device or browser when they log in from another one. Unfortunately, I have not been able to find ...