Sunday, 11 August 2024

React Hooks Error - Avoid to do mistake

 1.) Always use functional version to update state to update state proper


Example :-


function App(){


const[number,setNumber] = useState(0);

const increase =() => {

setNumber(number + 1)

}

const increaseAsync =() => {

setTimeout(() => {

setNumber((currentNumber)=> currentNumber + 1);//like this

setNumber(number +1)///not like this

},2000);

}

return (

‹div>

‹button onClick={increase)>Increase</button>

‹button onClick={increaseAsync)>IncreaeAsync</button>

<h1>{number}</h1>

</div>

);

}

export default App;

2.) Make proper State Initialization


Error :- Generate error like Cannot read properties of undefined ( reading 'name')

function App (){

//const [user, setuser] = useState()

const [user, setuser] = useState({

Username:"",

email:"",

images:[]

})

// user: {

//name:"john",

//email:"john@gmail.com",

//images: ["profile.png", "cover -png" ]

//}

return (

‹div>

<span>Username is: {user.name)</span>//not to use like this

<span>Username is: {user?.name)</span>//use like this 

<span>Username is: {user && user.name)</span>//or use like this

<span>Profile Picture is: {user.images[1])</span>//this gives error

//or you can inititalize state properly like below

//const [user, setuser] = useState({})//here our state is object

//

</div>

);

}

export default App;

3.) Update specific object property


function App() {

const [input, setInput] = useState("");

const [user, setUser] = useState ({

name: "john",

email: "john@gmail. com",

images: ["profile.png", "cover.png"],

}) ;

const changeUser = () => {

setUser ((prev) => ({ ...prev, name: input }));

};

console. log (user);

return (

‹div>

<h2>User: </h2>

‹input onChange={(e) => setInput (e. target.value)} placeholder="enter a new name.../>

‹button onClick={changeUser}>Change username</button>

‹span>Username is: {user.name)}</span>

‹/div>

}

4.) Change all inputs with a single onChange


import { useState ) from "react";

import "./style-css";


function App() {

const [user, setuser] = useState({

name: "",

surname:"",

username:"",

email: "",

password:"",

country: "",

city: "",

address: "",

})

const handleChange =(e)=>

setUser (prev=>({...prev,[e.target.name]: e. target.value}))

}

console.log(user);


return (

‹div>

‹form>

<input type="text" name="name" onChange={handlChange} />

<input type="text" name="surname" onChange={handlChange}/>

<input type="text" name="username" onChange={handlChange}/>

<input type="text" name="email" onChange={handlChange}/>

‹input type="text" name="password" onChange={handlChange}/>

<input type="text" name="country" onChange={handlChange}/>

<input type="text" name="city" onChange={handlChange}/>

<input type="text" name="address" onChange={handlChange}/>

‹button>Submit</button>

</form>

</div>

);

}

export default App;


5.) UseState vs useReducer when to use ?

---> when you have complex object then use useReducer hook


import { useState } from "react";

import "./style.css";


function App() {

const [product, setProduct] = useState({

title: ""

desc: "",

price: 0,

category: "",

tags: [],

images: {

sm:"",

md:"",

lg:"",

},

quantity: 0,

});

return (

<div>

<form>

<input type="text" placeholder="Title" />

<input type="text" placeholder="Desc" />

<input type="number" placeholder="Price" />

<p> Category: </p>

<select name="cars" id="cars">

<option value="sneakers">Sneakers</option>

<option value="tshirts">T-shirts</option>

<option value="jeans">Jeans</option>

</ select>

<p>Tags:</p>

<textarea placeholder="Seperate tags with comma" />

<div className="quantity">

<button>-</button>

<span>Quantity ({product. quantity}) </span>

<button>+</button>

</div>

</form>

</div>

);

};

export default App;


6.) Derive States Incorrectly


import "-/style.css";


function App() {


const [products, setProducts] = useState([

{id: 1, title: "black sneakers", quantity: 1},

{id: 2, title: "red t-shirt", quantity: 1},

{id: 3, title: "blue jeans", quantity: 1},

]);

const [selectedProduct, setSelectedProduct] = useState();


const increment = (id) => {

setProducts ((prev) => {

return prev.map((product)=> {

if (product. id === id) {

return {... product,quantity: product.quantity + 1 };

} else return product;

});

});

};

const handleChoose = (id)=> {

const product = products.find((p)=> p.id === id);

setSelectedProduct (product);

};


return (

<div>

<h4> All Products</h4>

{products.map((product)=> (

<div key={product.id}>

<span>{product. title}

<button onClick={() => handleChoose(product.id)}>Choose</button>

</span>

<div className="quantity">

<button>-</button>

<span>{product.quantity}</span>

<button onClick={() => increment(product.id)}>+</button>

</div>

</div>

))}

<h4>Selected Product</h4>

<span>{selectedProduct.title}</span>

<span>{selectedProduct.quantity}</span>

</div>

);

 };

 export default App;

 

 use like below

 

 

import { useState } from 'react';

import "./style.css";


function App() {

const [products, setProducts] = useState([

{ id: 1, title: "black sneakers", quantity: 1},

{ id: 2, title: "red t-shirt", quantity: 1 },

{ id: 3, title: "blue jeans", quantity: 1 },

]);

const [selectedId, setSelectedId] = useState(null);


const selectedProduct = products.find((p)=> p.id === selectedId);


const increment = (id) => {

setProducts ((prev) => {

return prev.map((product)=> {

if (product. id === id) {

return {... product, quantity: product.quantity + 1 };

} else return product;

});

});

};

const handleChoose = (id)=> {

setSelectedId(id);

};


return (

<div>

<h4> All Products</h4>

{products.map((product)=> (

<div key={product.id}>

<span>{product. title}

<button onClick={() => handleChoose(product.id)}>Choose</button>

</span>

<div className="quantity">

<button>-</button>

<span>{product.quantity}</span>

<button onClick={() => increment(product.id)}>+</button>

</div>

</div>

))}

<h4>Selected Product</h4>

<span>{selectedProduct?.title}</span>

<span>{selectedProduct?.quantity}</span>

</div>

);

};

 export default App;

 

 

7.) Don't use useState if not needed in form 


import { useRef } from "react";


function App() {

const emailRef = useRef()

const passwordRef = useRef()

function onSubmit(e) {

e.preventDefault()

console.log({ email:emailRef.current.value, 

  password : passwordRef.current.value

})

}

return (

<form onSubmit={onSubmit}>

<label htmlFor="email">Email</label>

<input ref={emailRef} type="email" id="email" />

<label htmlFor="password">Password</label>

<input ref={passwordRef} type="password" id="password" />

<button type="submit">Submit</button>

</form>

)

}

export default App


8.) State update are not immediate


"use client";


import { useState } from "react";


export default function Counter () {

const [count, setCount] = useState(0) ;

const handleClick = () => {

//Use prev value to update state value instead use count variable 

//setCount (count + 1)

//setCount (count + 1);

//setCount (count + 1);

//setCount (count + 1);

setCount ((prev) => prev + 1);

setCount ((prev) => prev + 1);

setCount ((prev) = prev + 1);

setCount ((prev) => prev + 1);

};

return (

<>

<button onClick={handleClick} className="bg-blue-500 px-4 py-2 text-white rounded mb">Click me<button>

<p>Count is : {count}</p>

</>

)

9.) Conditional Rendering 


use only one return statement and use below hooks (like useState and use Effect) 


export default function ProductCard(f id }) {

if (!id) {

return "No id provided";

}

return ‹section>{/* Product card... */}</section>;

}


use like below



"use client";


import { useEffect, useState } from "react";


export default function ProductCard({ id }) {

const [something, setSomething] = useState("blabla");

useEffect(() => (J, [something]);

return (

  <section>

{ !id ? "No id provided" :{ /* Product card... * / }}

  </section>

) ;

}


10.) Updating object State


"use client";


import { useState } from "react";


export default function User() {

const [user, setUser] = useState({name:"",city:"",age: 50 });

const handleChange =  (e) => {

//setUser((user.name = e.target.value));//not use like this because we have state is object not single value

setUser({

...user,//copy all old value object

name: e.target.value // overwrite new value to object for name property

});

//OR

//some user use like below as well

setUser(prev => ({

...prev,

name: e.target.value 

}));

//OR

//some user use like below as well

setUser(prev => {

return{

...prev,

name: e.target.value 

};

});

};

return (

<form>

<input type="text" onChange={handleChange} placeholder="Your name" /›

</form>

);

}


11.) Use object state instead of multiple smaller once


note :- Makje sure your object key is same as form input name property 

Example here for For FirstName --> object key is firstName and input namr is also firstName.


"use client";

import { useState } from "react";

export default function Form() {

const [form, setForm] = useState({

firstName: ""

lastName: ""

email: ""

password: ""

address: ""

zipCode: ""

}) ;

//Don't use like below function

/*const handleChange = e => {

setForm({ 

...form,

firstName : e.target.value

})

};*/

//Use Like below instead use above

const handleChange = (e) => {

setForm({ 

...form,

[e.target.name] : e.target.value

})

};

return (

<form className="flex flex-col gap-y-2"›

<input type="text" name="firstName" placeholder="first name" className="px-4 py-2" onChange={handleChange} />

<input type="text" name="lastName" placeholder="last name" className="px-4 py-2" onChange={handleChange} />

<input type="text" name="email" placeholder="email" className="px-4 py-2" onChange={handleChange}/>

<input type="text" name="password" placeholder="password" className="px-4 py-2" onChange={handleChange}/>

<input type="text" name="address" placeholder="address" className="px-4 py-2" onChange={handleChange}/>

<input type="text" name="zipCode" placeholder="zip code" className="px-4 py-2" onChange={handleChange}/>

<button className="bg-blue-500 px-4 py-2">Submit</button>

</form>

);

}

React Hooks - useState

useState is a react hook, which creates a "state variable" Which helps us to track state in components & updates the user interface when state changes.

Example - 1
import React, { useState } from "react";

import ReactDOM from "react-dom/client";


function FavoriteColor() {

  const [color, setColor] = useState("red");


  function changeColor(color) {

    setColor(color);

  }

  return (

    <>

      <h1>

        My favorite color is

        <div style={{ color: color }}>{color}</div>

      </h1>

      <button type="button" onClick={() => changeColor("blue")}>

        Blue

      </button>

      <button type="button" onClick={() => changeColor("red")}>

        Red

      </button>

      <button type="button" onClick={() => changeColor("pink")}>

        Pink

      </button>

      <button type="button" onClick={() => changeColor("green")}>

        Green

      </button>

    </>

  );

}

export default FavoriteColor;




















Example: 2 Instead use of multiple state use state object and 
Update data of State Object

import './App.css'

import { useState } from 'react';

function App()  {

const [car, setCar] = useState({

brand: "Ferrari",

model: "Roma",

year: "2023",

color: "red"

}) ;

const changeColor = () => {

        //setColor({color:”red”})//replace whole object which is not right way

        setCar((prev) => {

return {…prev, color:”blue”}

//instead of prev we can write any name

//we can preserve whole object data using …prev and then we can add new value to state object

})

}

return (

<> 

<h1>My {car.brand}</h1>

<h2>It is a {car.color} {car.model} from {car.year)</h2>

<button onClick={changeColor}>Blue</button>

</>

)

}

export default App



Example : 3 Increase counter with useState prev value

import './App.css';

function App()  {

const [count, setCount] = useState(0);

const increaseCount = () => {

setCount(count+1) //increase counter by 1

//but if you increase counter by 4 like below then also it will increase by 1 in UI- refer below snapshot-1.

setCount(count+1)//take initial value 0 +1 =1

setCount(count+1) //take initial value 0 +1 =1

setCount(count+1) //take initial value 0 +1 =1

setCount(count+1) //take initial value 0 +1 =1

//To update counter by 4 in UI side need to use prev value like below refer below snapshot-2

setCount((prev)=>{prev+1}))//take initial value 0 +1 = 1

setCount((prev)=>{prev+1}))//take prev value 1 +1 = 2

setCount((prev)=>{prev+1}))//take prev value 2 +1 = 3

setCount((prev)=>{prev+1}))//take prev value 3 +1 = 4

//you can also use count instead of prev like below

setCount((count)=>{count +1}))//take count initial value 0 +1 = 1

setCount((count)=>{count +1}))//take count prev value 1 +1 = 2

setCount((count)=>{count +1}))//take count prev value 2 +1 = 3

setCount((count)=>{count +1}))//take count prev value 3 +1 = 4

}

return (

<> 

<h1>Count : 0</h1>

<button onClick={increaseCount}>Increase</button>

</>

)

}

export default App












Sunday, 4 August 2024

React JS - Redux Flow Diagram and Steps

 








Steps of Redux
1.) Create Action
2.) Create Reducer
3.) Create Root Reducer ---> combine all reducer in one file
4.) Create Store
5.) Inject store to App component, wrap app with provider in app component and pass store as property
6.) use hook useSelector to directly access state---->used to display state value that are hold by redux store
7.) use hook useDispatch give directly dispatch access.
--> Send action by using dispatch method.
--> You can update state value by using useDispatch hook

Note: 

1.) MapStateToProps and mapDispatcchToProps are old redux method
2.) useDispatch and useSelector hook use in your normal component whereever you needed.



React Hooks - custom Hook

  v CustomHook Ø React allows us to create our own hook which is known as custom hook. Example – 1 localStorage Demo Step-1 Create ...