{"id":7394,"date":"2023-09-16T23:01:26","date_gmt":"2023-09-16T17:31:26","guid":{"rendered":"https:\/\/pheonixsolutions.com\/blog\/?p=7394"},"modified":"2023-09-16T23:08:20","modified_gmt":"2023-09-16T17:38:20","slug":"basic-guidelines-to-upload-an-image-in-react-js","status":"publish","type":"post","link":"https:\/\/pheonixsolutions.com\/blog\/basic-guidelines-to-upload-an-image-in-react-js\/","title":{"rendered":"Basic Guidelines to Upload an Image in React JS"},"content":{"rendered":"\n<p class=\"has-text-align-justify wp-block-paragraph\"><strong>Basic Guidelines to Upload an Image in React JS<\/strong><\/p>\n\n\n\n<p class=\"has-text-align-justify wp-block-paragraph\">Image uploads are a fundamental part of modern web development. Whether you&#8217;re building a social media platform, an e-commerce site, or a simple blog, the ability for users to upload images is a common requirement.<\/p>\n\n\n\n<p class=\"has-text-align-justify wp-block-paragraph\">Image uploads allows users to add images from their local devices to a website or an application. This functionality is crucial in various scenarios including user profile pictures, product images or sharing media content.<\/p>\n\n\n\n<p class=\"has-text-align-justify wp-block-paragraph\">Uploading images in a React.js application involves handling user interactions to select and upload images, managing the selected images, and possibly sending them to a server.<\/p>\n\n\n\n<p class=\"has-text-align-justify wp-block-paragraph\">React js provides state management and event handling features that can be used effectively for uploading images easily.<\/p>\n\n\n\n<p class=\"has-text-align-justify wp-block-paragraph\">Here we can consider a scenario where an image upload icon is given and when it is clicked, images from local machine can be selected by OnClick() function. Also the selected images are sent to server i.e.,uploading to database happens indirectly by trigerring another OnChange() function.<br><br>While selected images are simultaneously uploaded to server, there may arise a need to change or remove the selected images. For this scenario, a close icon can be added over the image and when the icon is clicked, remove function can be triggered and so the selected image can be removed from UI and also from the server.<br><br>The code to achieve this functionality can be as simple as follows:<br><br><strong>To select images:<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><em>&lt;img src={image} alt=\u201ddefault\u201d onClick={handleImageClick}\/&gt;<\/em><\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><em>&lt;input type =\u201dfile\u201d id =\u201dfileInput\u201d style ={{display : \u201cnone\u201d }} onChange ={handleFileChange}\/&gt;<\/em><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Here image is the icon placed and when it is clicked it allows user to select images from their local machine. Any number of images can be selected with this handleImageClick().<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><em>const handleImageClick =() =&gt; {<\/em><\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><em>document.getElementById(\u2018fileInput\u2019).click();<\/em><\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><em>};<\/em><\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Storing the selected Image:<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Once the image is selected, it needs to be converted to proper format that the server accepts. For this step, look at the below code.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><em>const handlefilechange = (event) =&gt; {<\/em><\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><em>const file = event.target.files[0];<\/em><\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><em>const imageUrl = URL.createOjectURL(file);<\/em><\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><em>setselectedImages(prevImages =&gt; [\u2026prevImages, imageUrl]);<\/em><\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><em>List(prevList =&gt; [\u2026prevList, file]);<\/em><\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><em>uploadImage(file);<\/em><\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><em>}<\/em><\/p>\n\n\n\n<p class=\"has-text-align-justify wp-block-paragraph\">handlefilechange function is called when the user selects an image. It receives the event object, which contains information about the selected file. Here&#8217;s what this function does:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>It extracts the selected file using event.target.files[0]<\/li>\n\n\n\n<li>It creates a URL for the selected file using URL.createObjectURL(file)<\/li>\n\n\n\n<li>It updates the state using useState hooks to store the selected images and the list of files.<\/li>\n\n\n\n<li>It calls uploadimage(file) to upload the image to the server.<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Uploading the image to server:<\/strong><\/p>\n\n\n\n<p class=\"has-text-align-justify wp-block-paragraph\">Here the selected and stored images are sent to the server using API call. The response data can be again set to state variable for accessing throughout the application.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><em>uploadImage = (file ) = &gt; {<\/em><\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><em>const formData = new formData();<\/em><\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><em>formData.append(\u2018file\u2019,file);<\/em><\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><em>\/\/upload file to server through any API call methods<\/em><\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><em>Const fileUrl = apiresponse.file_url;<\/em><\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><em>\/\/Update the list with the file URL<\/em><\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><em>}<\/em><\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Removing the selected\/uploaded image:<\/strong><\/p>\n\n\n\n<p class=\"has-text-align-justify wp-block-paragraph\">This function allows users to remove a selected image from the list. It filters the selectedImages array to exclude the image at the specified index. This can be achieved by placing a Clear icon over the images.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><em>&lt;button<\/em><\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><em>onClick = {() =&gt; handleRemoveImaege(index)}<\/em><\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><em>style ={{<\/em><\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><em>position : \u201cabsolute\u201d,<\/em><\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><em>top : \u201c0px\u201d,<\/em><\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><em>right : \u201c-1px\u201d,<\/em><\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><em>border : \u201cnone\u201d,<\/em><\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><em>cursor : \u201cpointer\u201d,<\/em><\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><em>color : \u201cwhite\u201d,<\/em><\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><em>}}<\/em><\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><em>&gt;&nbsp;<\/em><\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><em>&lt;ClearIcon \/&gt;<\/em><\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><em>&lt;\/button&gt;<\/em><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">ClearIcon can be imported from materailui\/icons.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><em>const handleRemoveImage = ( index) =&gt; {<\/em><\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><em>setSelectedImages(prevImages =&gt; prevImages.filter((_,i)=&gt; I !== index));<\/em><\/p>\n\n\n\n<p class=\"has-text-align-justify wp-block-paragraph\"><em>};<\/em><br><br><strong>Rendereing the images:<\/strong><br><br>To render the images , we need to map over the selectedimages array and display the images as &lt;img&gt;&nbsp; as follows:<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><em>{selectedImages.map((image,index) =&gt; (<\/em><\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><em>&lt;div key = {index}&gt;<\/em><\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><em>&lt;img<\/em><\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><em>src = {image}<\/em><\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><em>alt = {`Selected $ {index}`}<\/em><\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><em>\/&gt;<\/em><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Basic Guidelines to Upload an Image in React JS Image uploads are a fundamental part of modern web development. Whether [&hellip;]<\/p>\n","protected":false},"author":503,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"site-sidebar-layout":"default","site-content-layout":"","ast-site-content-layout":"default","site-content-style":"default","site-sidebar-style":"default","ast-global-header-display":"","ast-banner-title-visibility":"","ast-main-header-display":"","ast-hfb-above-header-display":"","ast-hfb-below-header-display":"","ast-hfb-mobile-header-display":"","site-post-title":"","ast-breadcrumbs-content":"","ast-featured-img":"","footer-sml-layout":"","ast-disable-related-posts":"","theme-transparent-header-meta":"","adv-header-id-meta":"","stick-header-meta":"","header-above-stick-meta":"","header-main-stick-meta":"","header-below-stick-meta":"","astra-migrate-meta-layouts":"default","ast-page-background-enabled":"default","ast-page-background-meta":{"desktop":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"tablet":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"mobile":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""}},"ast-content-background-meta":{"desktop":{"background-color":"var(--ast-global-color-4)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"tablet":{"background-color":"var(--ast-global-color-4)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"mobile":{"background-color":"var(--ast-global-color-4)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""}},"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":false,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2},"jetpack_post_was_ever_published":false},"categories":[1022],"tags":[],"class_list":["post-7394","post","type-post","status-publish","format-standard","hentry","category-web-architecture","psol-cat-web-architecture"],"jetpack_publicize_connections":[],"jetpack_shortlink":"https:\/\/wp.me\/phn2x7-1Vg","jetpack_sharing_enabled":true,"jetpack_featured_media_url":"","_links":{"self":[{"href":"https:\/\/pheonixsolutions.com\/blog\/wp-json\/wp\/v2\/posts\/7394","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/pheonixsolutions.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/pheonixsolutions.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/pheonixsolutions.com\/blog\/wp-json\/wp\/v2\/users\/503"}],"replies":[{"embeddable":true,"href":"https:\/\/pheonixsolutions.com\/blog\/wp-json\/wp\/v2\/comments?post=7394"}],"version-history":[{"count":0,"href":"https:\/\/pheonixsolutions.com\/blog\/wp-json\/wp\/v2\/posts\/7394\/revisions"}],"wp:attachment":[{"href":"https:\/\/pheonixsolutions.com\/blog\/wp-json\/wp\/v2\/media?parent=7394"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/pheonixsolutions.com\/blog\/wp-json\/wp\/v2\/categories?post=7394"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/pheonixsolutions.com\/blog\/wp-json\/wp\/v2\/tags?post=7394"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}