REACT-HOOKS (Usestate) & ADV. TOPICS
REACT-HOOKS (Usestate) & ADV. TOPICS
TOPICS
PROJECT START UP
HOOK DEFINITION
GENERAL RULES OF HOOKS
USE STATE HOOK EXAMPLE (TO TRIGGER RE-RENDER)
THE USE STATE HOOK
INITIAL RENDER & RE-RENDER
USESTATE HOOK DEMO (DYNAMIC LIST OF PEOPLE W/ BTNS TO REMOVE A PERSON
OR ALL PPL)
Automatic Batching
USESTATE WITH OBJS
USESTATE DOES NOT IMMEDIATELY MUTATE THE STATE
PASSING A FN TO USESTATES SET FN TO MUTATE STATE IMMEDIATELY
PROJECT START UP
1. Copied his Repo Code (“03-ADV-React”) into my Smilga DIR
2. VSCODE 1. npm install 2. npm run dev
3. ALT/Clk L/Host Link in Terminal to Open PG
*At the Start of ea. Tutorial = have to Import the Starter into App.jsx
HOOK DEFINITION
*”A Hook is a special function that lets you “hook into” React features. “
*There are a few ways that you can trigger a re-render in a React component:
1. By changing the component's state or props. When the component's state or props
change, React will re-render the component to reflect these changes.
2. When the parent element re-renders, even if the component's state or props have not
changed.
// Component
const UseStateArray = () => {
// Destr. the useState [] - into its VAR & its FN
// Initialise its Inital State to that of "data" (which we Imported)
// "data" is Reference to an OBJ w/ PROPs = 1. id 2. names (of ppl)
// So: "people" VARs VAL is the OBJ
const [people, setPeople] = useState(data);
setPeople([]);
};
return (
<div>
{/* // Pass ea. OBJ (as "person") Into MAP */}
{people.map((person) => {
// Destr. the VALs- From OBJ ("person") PROPs
const { id, name } = person;
//
return (
<div key={id}>
<h4>{name}</h4>
{/* doesnt get Invoked straight away bc its Inside an ANon FN */}
<button type="button" onClick={() => removeItem(id)}>
remove
</button>
</div>
);
})}
<button
type="button"
style={{ marginTop: '2rem' }}
className="btn"
onClick={clearAllItems}
>
clear items
</button>
</div>
);
};
Automatic Batching
*In React, "batching" refers to the process of grouping multiple state updates into a single
update. This can be useful in certain cases because it allows React to optimize the
rendering of your components by minimizing the number of DOM updates that it has to
perform.
*By default, React uses a technique called "auto-batching" to group state updates that
occur within the same event loop into a single update. This means that if you call the
state update function multiple times in a short period of time, React will only perform a
single re-render for all of the updates.
*React 18 ensures that state updates invoked from any location will be batched by
default. This will batch state updates, including native event handlers, asynchronous
operations, timeouts, and intervals.
This means that - these STATE UPDATES are Updated via AUTO-BATCHING
const displayPerson = () => {
//
setName('john');
setAge(28);
setHobby('screaming');
};
USESTATE WITH OBJS
*bc the VAR that useState uses will Hold either a VAL (if only 1 VAL) OR an ARR (if
more than 1 VAL) - sometimes we need it to Hold an OBJ
// PASS IN the VAL that is Currently in the STATE (In STATES “value”) as
currentState
setValue((currentState) => {