Skip to main content

Posts

Multiple Image Uploads in React JS with NodeJs And MongoDB

In this blog we will discuss Multiple Image Uploads in React JS with NodeJs And MongoDB. For the frontend we will have an Input tag with multiple attribute to take multiple inputs in React JS example -   < input         onChange = { ( e ) => {           setImage ( e . target . files )         } }         multiple         type = "file"       /> We will the bind the onChange method with the state to save the e.target.files (FileList Array) const [ image , setImage ] = useState ([]) We will create a button to handle API submit     < button onClick = { handleSubmit } > SUBMIT </ button > So the code on App.js Becomes : import { useState } from "react" import axios from 'axios' function App () {   const [ image , setImage ] = useState ([])   const [ category , setCategory ] = useState ( 'electronics' )   c...

How to use & create .env Files in React JS

 We need .env files to save data that can be changed even when the project is deployed on server. If we save the constants in a constant.js file than this file will also be deployed on the server. And If we want to make some chnages in that we need to deployed the code of the project again. When we call apis in React JS or any other Framework we need Base Url and endpoint of the Api like If we are running our project on our local system. The Base url will be http://localhost:3000 and endpoint for login api will be something like /api/login. But if we deploy the project on server its endpoint remains same but its Base url is changed. So to manage the situation we make constant in .env file and use it in the whole project code. And deploy the project on server. Once project is deployed we have option to change the Base url of the project using environment variables concept. Example :  REACT_APP_BASE_URL =https://reqres.in REACT_APP_API_KEY =ghdfhgc344b54b Note - We can install p...

Stop the Image Slider on Mouse Hover & start slider again when mouse is out of the image in React js

To achieve this functionality we need to save the setInterval id in state , With the help of id we can stop the setInterval . We will have two functions then one to start the setInterval or image slider . Another to stop the Image slider or setInterval function. Example :    const start = () => {     console . log ( 'start' )     const _start = setInterval (() => {       setSelectedImage ( selectedImage => selectedImage < 3 ? selectedImage + 1 : 0 )     }, 4000 )     setId ( _start )   }   const stop = () => {     console . log ( 'stop' )     clearInterval ( id )   } Now we need to call these functions on Mouse Over and Mouse Out Events Like :   < img         onMouseOut = { () => start () }         onMouseOver = { () => stop () }         width = { 810 } height = { 400 } src = { allI...

Create A Date Range Picker in React JS (By Saifi Code)

 To Create  A Date Range Picker in React JS , need to install following packages :  npm i antd npm i moment Step 1 : import DatePicker from antd and get RangePicker from this package Step 2 : In the JSX Use the package , you will start get picker ui Example :  To get the values use onChange method and save values in state  Try to console the state to check if the values are updating App.js File : import 'antd/dist/antd.css' ; import { DatePicker } from 'antd' ; import { useState } from 'react' ; import moment from 'moment' ; const { RangePicker } = DatePicker ; function App () {   const [ dates , setDates ] = useState ([])   console . log ( dates )     return (     < div style = { { margin : 20 } } >       < RangePicker         onChange = { ( values ) => {                     setDates ( values . map ( item => { ...

Shopping Cart React.js Project with Explanation✅ [ Simple Ecommerce Website using Reactjs ]

Hello guys in this video we will discuss shopping cart web app.  first we need to install the basic react app using - npx create-react-app app-name The folder structure should be containing three files  in components folder as below : In App.js :  import './App.css' ; import Header from './components/Header' ; import ProductList from './components/ProductList' ; import CartList from './components/CartList' ; import { useState } from 'react' ; function App () {   const [ product , setProduct ] = useState ([     {       url : 'https://rukminim1.flixcart.com/image/300/300/l51d30w0/shoe/z/w/c/10-mrj1914-10-aadi-white-black-red-original-imagft9k9hydnfjp.jpeg?q=70' ,       name : 'TRQ White Shoes' ,       category : 'Shoes' ,       seller : 'AMZ Seller Ghz' ,       price : 1999     },     {       url : 'https://5.imimg.com/data5/KC/PC...

How to make cascading dropdowns in React JS

 We can make dropdowns in React JS by following method : Step 1 : Take a list of states and cities object related to every state or we can get this data using apis. Step 2 : Loop over the states data to show dropdown related to All States. Step 3 : Now save the selected state. Step 4 : Based on selected state value , get the corresponding cities and show them in dropdown as follows : In App.js  import { useState } from "react" ; import './App.css' ; function App () {   const states = [ 'UP' , 'Delhi' , 'Gujrat' ]   const cities = {     'UP' : [ 'f' , 'g' , 'l' ],     'Delhi' : [ 'a' , 'b' ],     'Gujrat' : [ 'tr' , 'trt' , 'rtt' ],   }   const [ selectedState , setSelectedState ] = useState ( '' )   console . log ( selectedState )   return (     < div >       CASCADING DROPDOWNS :       < select onChange = { ( e ) => { set...

How to save data in localstorage in React JS [ Use of stringify and parse methods ]

 If we want to save string in localstorage , we can save simply like    localStorage . setItem ( 'str' , 'hello local' ) and we can get data from local storage as follows :       let str = localStorage . getItem ( 'str' )       console . log ( str) But if we want to save data in array and objects format , we need to convert array and objects in string format using stringify method.  When we need to get the saved array and objects we can convert stringified array and objects using parse method. Example below :  in the App.js : function App () {   return (     < div >       Hello from App.js       < button onClick = { () => {         localStorage . setItem ( 'arr' , JSON . stringify ([ 1 , 2 , 3 , 4 ]))         localStorage . setItem ( 'obj' , JSON . stringify ({ name : 'name' , address : 'address1' }))       } } ...