Image Upload in React: Build a LinkedIn-Style Feature (2026)

Create a LinkedIn-style image upload feature in React with previews, validation, and progress indicators to enhance user experience.

Image Upload in React: Build a LinkedIn-Style Feature (2026)

Image Upload in React: Build a LinkedIn-Style Feature (2026)

In this tutorial, you'll learn how to create an image upload feature in React that mimics the functionality found in LinkedIn's profile upload. This feature is particularly useful for applications that require user profile image management, and mastering it can enhance the user experience of your app.

Key Takeaways

  • Understand how to set up a React project with image upload capabilities.
  • Learn to integrate image previews before final upload.
  • Implement error handling for file uploads.
  • Enhance the UX with progress indicators and validation.

Prerequisites

Before diving into the code, ensure you have the following:

  • Basic knowledge of React (v18.0.0 or later).
  • Node.js and npm installed on your machine.
  • Familiarity with CSS for styling components.
  • A basic understanding of how file uploads work in web applications.

Step 1: Set Up Your React Project

First, we need to set up a new React project. Open your terminal and run the following commands:

npx create-react-app linkedin-image-upload
cd linkedin-image-upload
npm start

This will create a new React application and start the development server.

Step 2: Create the Image Upload Component

Let's create a component that will handle the image upload. In the src directory, create a new file named ImageUpload.js and add the following code:

import React, { useState } from 'react';

const ImageUpload = () => {
  const [image, setImage] = useState(null);
  const [preview, setPreview] = useState(null);

  const handleImageChange = (e) => {
    const file = e.target.files[0];
    setImage(file);
    if (file) {
      const reader = new FileReader();
      reader.onloadend = () => {
        setPreview(reader.result);
      };
      reader.readAsDataURL(file);
    }
  };

  return (
    
      
      {preview && }
    
  );
};

export default ImageUpload;

This component uses the FileReader API to create a preview of the image before uploading.

Step 3: Style Your Component

To make the component look more appealing, add the following CSS to your App.css:

.upload-container {
  display: flex;
  flex-direction: column;
  align-items: center;
  margin: 20px;
}

.image-preview {
  margin-top: 10px;
  width: 200px;
  height: 200px;
  object-fit: cover;
  border-radius: 50%;
}

This styling centers the image preview and gives it a circular shape, similar to LinkedIn's profile images.

Step 4: Implement Error Handling and Validation

To handle errors and validate the file type and size, modify the handleImageChange function:

const handleImageChange = (e) => {
  const file = e.target.files[0];
  if (!file) return;
  if (!file.type.startsWith('image/')) {
    alert('Please upload a valid image file.');
    return;
  }
  if (file.size > 2000000) { // 2 MB limit
    alert('File size is too large. Maximum size is 2 MB.');
    return;
  }
  setImage(file);
  const reader = new FileReader();
  reader.onloadend = () => {
    setPreview(reader.result);
  };
  reader.readAsDataURL(file);
};

This code checks for valid image types and restricts file size to 2 MB.

Step 5: Add a Progress Indicator

To give feedback during the upload process, we can add a progress indicator. This requires a backend service to handle uploads, but we'll simulate the process here:

const [uploading, setUploading] = useState(false);
const [progress, setProgress] = useState(0);

const simulateUpload = () => {
  setUploading(true);
  setProgress(0);
  const interval = setInterval(() => {
    setProgress((oldProgress) => {
      if (oldProgress === 100) {
        clearInterval(interval);
        setUploading(false);
        return 100;
      }
      return Math.min(oldProgress + 10, 100);
    });
  }, 200);
};

Add a button to start the simulated upload and display the progress:

{image && !uploading && (
  Upload
)}
{uploading && Uploading: {progress}%}

This simulates an upload process by incrementing the progress over time, providing visual feedback.

Common Errors/Troubleshooting

Here are some common issues you might encounter:

  • FileReader API Error: If the image preview doesn't work, ensure your browser supports the FileReader API.
  • Invalid File Type: Check that the file type is correctly validated to avoid non-image uploads.
  • Progress Indicator Not Updating: Ensure that the state updates are correctly implemented and that the interval is cleared once 100% is reached.

Conclusion

By following these steps, you can create a robust image upload feature in React that mimics LinkedIn's profile picture upload. This feature not only improves the user experience but also provides essential validation and feedback mechanisms that are critical in modern web applications. Continue to iterate and improve upon this foundation by integrating real backend services for file uploads and enhancing security measures.

Frequently Asked Questions

Can I use this code with other frameworks?

While this tutorial is focused on React, the fundamental concepts of file handling and validation are applicable to other frameworks like Angular or Vue.js.

How can I handle large file uploads?

Consider using chunked uploads or limiting the file size on the client side to manage large files effectively.

What if my preview is not displaying?

Ensure your browser supports the FileReader API and that your image file is not corrupted.

Can I customize the styling?

Yes, you can modify the CSS to suit your application's design needs.