Skip to main content
In this lesson we’ll build a simple, responsive React UI to upload an image, choose an output quality (0–100), and post both to an API endpoint that performs image optimization. The example uses React 18+ createRoot (see the React docs) and a Vite dev server for local development.

1. Entrypoint: index.js / index.jsx

Ensure your entry file mounts the app with the React 18+ API. This minimal entry file uses the createRoot API:
When running the Vite dev server you should see output similar to:

2. App.jsx — UI, upload form, and submit handler

Open App.jsx. We’ll create a compact form that accepts:
  • A file input for an image,
  • A quality slider (0–100),
  • A submit button that posts the file and quality as FormData to your optimization API.
Below is a full, cleaned-up App.jsx implementation with state handling, uploading, and basic error handling:
Tips:
  • Add name="image" to the file input so server frameworks reliably access the uploaded file.
  • Provide user feedback with loading state so users know the optimization is in progress.
Do not set the Content-Type header manually when sending FormData. The browser sets the multipart boundary automatically; setting Content-Type yourself will break the request.

3. Form data and server expectations

When sending the upload, we use FormData:
  • Field image: the uploaded file
  • Field quality: numeric quality value (0–100)
Use the table below as a quick reference for the request payload: If your backend returns:
  • JSON with a result URL: parse with await response.json() and use the URL to preview or download.
  • Binary image data: use await response.blob() and create an object URL via URL.createObjectURL(blob).

4. Styling: App.css

Apply these CSS rules to create a centered, responsive form with a dashed file input and a full-width slider:

5. UX expectations

With the markup, handler, and CSS applied you now have a centered, responsive form that:
  • Lets the user choose an image file,
  • Lets the user select an output quality with a slider (0–100),
  • Posts the file and quality to your API for processing.
If the file input appears to overflow or sit outside the dashed boundary, the box-sizing: border-box rule and the input width settings should correct that. Once the UI is in place you can:
  • Change the page background and accent colors to match your brand,
  • Add an inline preview for the selected file before upload,
  • Show an optimized-image preview after the server responds (either via a returned URL or by creating an object URL from the returned blob).
A browser window shows an "Image Optimizer" web app with an upload box, a quality slider at 52% and an "Optimize Image" button centered on a dark background. Behind the browser are code editor panels and a project file tree visible.
Now that the React application is running and visually organized, the next step is wiring the frontend to your backend optimization endpoint so uploads are processed and the optimized image is returned to the user.

Watch Video