const btnClasses=`${classes.button} ${classes.bump}`;//adding styles
Monday, October 31, 2022
React Adding styles function
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
Tuesday, October 18, 2022
React Input.js COmponent --CART proj
input.js
React: passing data/keys to childs
1.Available Meals:(Result data)
Monday, October 17, 2022
React: Card.js
Card.js
Saturday, October 15, 2022
React: Filling user input box misses highlights that input on submit. (order top to bottom)
129 lec
diving into forward refs
React: creating component for input- for reusable input box
Login.js
Initially
Friday, October 14, 2022
React :logout separate component using createcontext
auth-context.js
Thursday, October 13, 2022
React: usecontext for islogin, islogout (showing tabs if user login)
-----------------------------------------------auth-context.js--------------------------------------------------
import React from "react";
const AuthContext= React.createContext({
isLoggedIn:false
})
export default AuthContext;
-----------------------------------------------app.js-----------------------------------------------------------
HERE NEED TO ADD AS PROVIDER ie.,AuthContext.Provider
<AuthContext.Provider value={{isLoggedIn:isLoggedIn}}>
<MainHeader onLogout={logoutHandler} />
{/*1 <MainHeader isAuthenticated={isLoggedIn} onLogout={logoutHandler} /> */}
<main>
{!isLoggedIn && <Login onLogin={loginHandler} />}
{isLoggedIn && <Home onLogout={logoutHandler} />}
</main>
</AuthContext.Provider>
----------------------------------------------Navigation.js-----------------------------------------------------
1. with use context hook.............
const Navigation = (props) => {
const ctx= useContext(AuthContext); //changes: newly added this and imported useContext
return (
// <AuthContext.Consumer> //Removed these 3 lines
// {(ctx)=>{
// return (
<nav className={classes.nav}>
<ul>
{ctx.isLoggedIn && (
<li>
<a href="/">Users</a>
</li>
)}
{/* {props.isLoggedIn && ( */}
{ctx.isLoggedIn && (
<li>
<a href="/">Admin</a>
</li>
)}
{ctx.isLoggedIn && (
<li>
<button onClick={props.onLogout}>Logout</button>
</li>
)}
</ul>
</nav>
// )
// }}
// {/* will get data of auth context comp */}
// </AuthContext.Consumer>
--------------------WITHOUT usecontext hook----------------------------------
HERE NEED TO ADD AS CONSUMER ie., <AuthContext.Consumer>
const Navigation = (props) => {
return (
<AuthContext.Consumer>
{(ctx)=>{
return (<nav className={classes.nav}>
<ul>
{ctx.isLoggedIn && (
<li>
<a href="/">Users</a>
</li>
)}
{/* {props.isLoggedIn && ( */}
{ctx.isLoggedIn && (
<li>
<a href="/">Admin</a>
</li>
)}
{ctx.isLoggedIn && (
<li>
<button onClick={props.onLogout}>Logout</button>
</li>
)}
</ul>
</nav>)
}}
{/* will get data of auth context comp */}
</AuthContext.Consumer>
);
};
React: useReducer
# useReducer
UseReducer used for more complex forms, ex; step forms prev step to be passed to next step in form
const [state, dispatchFn]=useReducer(reducerFn,initialState, initFn)
1@state::: The state snapshot used in the component re-render/re-evaluation cycle
2@dispatchFn::: A function that can be used to dispatch a new action (ie,trigger an update of the state)(ie., to update that above state snapshot)
3@reducerFn::: (prevState, action)=>newState A function that is triggered automatically once an action is dispatched(via dispatchFn())-it receives that the latest state snapshot and should return the new, updated state. (ie, anytime new action is performed this function triggers)
4@initialState::: initial state
5@initFn::: A function to set the initial state programatically.
NOTE :
Adding Nested Properties As Dependencies To useEffect
In the previous lecture, we used object destructuring to add object properties as dependencies to useEffect().
const { someProperty } = someObject;
useEffect(() => {
// code that only uses someProperty ...
}, [someProperty]);
This is a very common pattern and approach, which is why I typically use it and why I show it here (I will keep on using it throughout the course).
I just want to point out, that they key thing is NOT that we use destructuring but that we pass specific properties instead of the entire object as a dependency.
We could also write this code and it would work in the same way.
useEffect(() => {
// code that only uses someProperty ...
}, [someObject.someProperty]);
This works just fine as well!
But you should avoid this code:
useEffect(() => {
// code that only uses someProperty ...
}, [someObject]);
Why?
Because now the effect function would re-run whenever ANY property of someObject changes - not just the one property (someProperty in the above example) our effect might depend on.
Monday, October 10, 2022
React: UseEffect Types and working flow
Runs Every Time:
Runs Only Once:
3.
Effect Running for password:
Password Effect cleanup runs first (ie, return statement runs first)(first console msg loads onload webpage ie, pass effectr runnig and checkig validity)
order:
Saturday, October 8, 2022
React: Error Modal popup | Created div's in Index.html
Modal popups is hidden by default,(coded inside nested div's ) so instead of using it in root div,
created new divs, in index.html in public folder
Step 1 :In react project: in index.html added below lines
<div id="backdrop-root"></div>
<div id="modal-root"></div>
<div id="root"></div>
ErrorModal code:
pushing to above new created div's
Old code ErrorModal.js
Use Wrapper to avoid empty div's for grouping
After using wrapper instead of div, grouping ignored wrapper tag.
created wrapper for ignoring grouping div, (for performance increase)
React: Sending data to siblings
1.React form component
2.calling above compoent
note: passing data to sibling.
---------------------------------
NewExpense.js file:
<ExpenseForm onSaveExpenseData={onSaveExpenseDataHandler}></ExpenseForm>
---------------------------------
ExpenseForm.js:
props.onSaveExpenseData(expenseData); // calling one js file to other(newexpense.js)
setEnteredTitle("");
setEnteredAmount("");
setEnteredDate("");
};
---------------------------------
calling expenseform in app.js
for appending form data to app.js file arr.
pushing data to parent node
props.onAddExpense(expenseData) // lifting up date to parent app.js
Use Card styling for specific tag.
Card.js
//For Dynamic below code cssClassName as ClassName default // return <div className={`${classes.card} ${props.cssClassName}`}>{props.children}</div>