Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

react open popup to upload image file

import React, { useEffect, useRef, useState } from 'react';

// Specify camera icon to replace button text 
import camera from '../../../assets/images/camera.svg'; // replace it with your path

// Specify your default image
import defaultUser from '../../../assets/images/defaultUser.svg'; // replace it with your path

// Profile upload helper

const HandleImageUpload = () => {
  // we are referencing the file input
  const imageRef = useRef();

  // Specify the default image
  const [defaultUserImage, setDefaultUserImage] = useState(defaultUser);
  
  // On each file selection update the default image
  const [selectedFile, setSelectedFile] = useState();

  // On click on camera icon open the dialog
  const showOpenFileDialog = () => {
    imageRef.current.click();
  };

  // On each change let user have access to a selected file
  const handleChange = (event) => {
    const file = event.target.files[0];
    setSelectedFile(file);
  };

  // Clean up the selection to avoid memory leak
  useEffect(() => {
    if (selectedFile) {
      const objectURL = URL.createObjectURL(selectedFile);
      setDefaultUserImage(objectURL);
      return () => URL.revokeObjectURL(objectURL);
    }
  }, [selectedFile]);

  return {
    imageRef,
    defaultUserImage,
    showOpenFileDialog,
    handleChange,
  };
};

// Image component
export const ItemImage = (props) => {
  const {itemImage, itemImageAlt} = props;
  return (
    <>
      <img
        src={itemImage}
        alt={itemImageAlt}
        className="item-image"
      />
    </>
  );
};

// Button with icon component
export const CommonClickButtonIcon = (props) => {
  const {
    onHandleSubmitForm, iconImageValue, altImg,
  } = props;
  return (
    <div className="common-button">
      <button
        type="button"
        onClick={onHandleSubmitForm}
        className="button-image"
      >
        <img
          src={iconImageValue}
          alt={altImg}
          className="image-button-img"
        />
      </button>
    </div>
  );
};

export const MainProfileForm = () => {
  const {
    defaultUserImage,
    handleChange,
    imageRef,
    showOpenFileDialog,
  } = HandleImageUpload();

  return (
    <div className="edit-profile-container">

      <div className="edit-profile-image">
        <ItemImage
          itemImage={defaultUserImage}
          itemImageAlt="user profile picture"
        />
        <CommonClickButtonIcon // Notice I omitted the text instead used icon
          onHandleSubmitForm={showOpenFileDialog}
          iconImageValue={camera}
          altImg="Upload image icon"
        />
        <input
          ref={imageRef}
          type="file"
          style={{ display: 'none' }}
          accept="image/*"
          onChange={handleChange}
        />
      </div>
    </div>
  );
};
Comment

PREVIOUS NEXT
Code Example
Javascript :: in node.js with express how to remove the query string 
Javascript :: fireOnChange 
Javascript :: string split into three non empty combination js 
Javascript :: Pass 3 of the same thing to ExpressJS with a form 
Javascript :: style dropdown react native picker 
Javascript :: C# Convert Json File to DataTable using Newtonsoft.Json DLL 
Javascript :: filter a object array tree javascript 
Javascript :: saves javascript 
Javascript :: show code in console very good 
Javascript :: json array form to list object java 
Javascript :: react state based router 
Javascript :: function x(a) vs function x(...a) the difference 
Javascript :: phaser move towards object 
Javascript :: 120. Triangle - JavaScript Solution With Explanation 
Javascript :: javascript code to decide even or odd number in html using visual studio 
Javascript :: Self Invoking Function Tip 
Javascript :: Javascript Area When Base and Height is Known 
Javascript :: parseint javascript online 
Javascript :: registration page validation in react 
Javascript :: If you wish to set a method equal to another method in the class 
Javascript :: console.log(number++); console.log(++number); console.log(number); 
Javascript :: NavBar with divs 
Javascript :: simple express server responce html css js 
Javascript :: how to get on hnage input before clicking off 
Javascript :: iconbuttons onclick redirect to another page on react 
Javascript :: unknown set of argument 
Javascript :: how to scroll element in javascript 
Javascript :: text inside image react native 
Javascript :: javascript alarm 
Javascript :: subarray javascript 
ADD CONTENT
Topic
Content
Source link
Name
1+2 =