used to show err status msg
Thursday, November 3, 2022
Wednesday, November 2, 2022
React: class component
import {Component } from 'react'; //-------------added this line
React useMemo() performance
https://www.w3schools.com/react/react_usememo.asp
it compares prev state with new state, if no change found then usememoze value is used directly
React: Usecallback & usememo--To improve performance
v:157: usecallback
use it when required instead of running every time.
(instead of running all the states,or all data in function, we can control it with usecallback, triggered only when requeired, check below code, 2 buttons added first click on paragraph button tht doesnt work, when clicking on first buttong and then clik on 2nd it works and it triggers, check with console)
function App(){
const [showParagraph, setShowParagraph]=useState(false);
const [allowToggle, setAllowToggle]=useState(false);
const toggleParagraphHandler=useCallback(()=>{
if(allowToggle){
setShowParagraph((prevShowParagraph)=>!prevShowParagraph);
}
},[allowToggle]);
const allowToggleHandler=()=>{
setAllowToggle(true);
}
return <Fragment>
<Button onClick="allowToggleHandler">Allow Toggling</Button>
<Button onClick="toggleParagraphHandler">Toggle Paragraph</Button>
</Fragment>
}
Monday, October 31, 2022
React Adding styles function
const btnClasses=`${classes.button} ${classes.bump}`;//adding styles
Sunday, October 30, 2022
React Context: use context instead of passing data with usestate to nested components
Readme
1. create Cart-Context.js with default structure (ie, **{items:[],totalAmount:0,addItem:(item)=>{},removeItem:(id)=>{}}**)
2. create CartProvider.js with above Cart-Context default obj structure,using it with dynamic values
{ items:[],totalAmount:0, addItem:addItemToCartHandler,removeItem:removeItemFromCartHandler },
ii.Create reducer and dispatch data from functions with addItemToCartHandler, removeItemFromCartHandler
iii.create usestate and add reducer to usestate
iv.<CartContext.Provider value={cartContext}> {props.children} </CartContext.Provider>
3.In app.js <CartProvider> <Header onShowCart={showCartHandler}/>... </CartProvider>
4.In header.js <header className={classes.header}><HeaderCartButton onClick={props.onShowCart}></HeaderCartButton></header>
5.HeaderCartButton.js :
const HeaderCartButton = (props) => {
const cartCtx= useContext(CartContext);
const numberOfCartItems= cartCtx.items.reduce((curNumber, item)=>{
return curNumber+item.amount;
},0);
return (
<Fragment>
<button className={classes.button} onClick={props.onClick}>
{/* 3. for popopening */}
<span className={classes.icon}>
<CartIcon/>
</span>
<span>Cart</span>
<span className={classes.badge}>{numberOfCartItems}</span>
</button>
</Fragment>
);
};
_________________________________________________________
Cart-Context.js
export default CartContext
2.CartProvider.js
export default CartProvider;
3.App.js
export default App;
4.Header.js
Saturday, October 22, 2022
React: passing data from multiple child to parent -
App.js
Header.jsHeaderCartButton.js