React Notes
React Notes
2.
//The JSX is converted to Pure React by babel
JSX is cleaner then the Pure React
(i.e (JSX) return (
<footer>{new Date().toLocaleTimeString()} We're currently open</footer>
);
//(Pure React) return React.createElement("footer", null , "We're currently
open!"))
3.
///when writing inline stlye in react for styling componenet the first you should
enter js mode {} and then inside it you should make an object
that of the style
4.
When we import the file like css into our application then it is the webpack that
will take
care of taking the css styles out of the css file and injecting it into our
application
5.
Always treat state as immutable in react dont mutate it because it is a bad
practice so always use the setFunction
in state dont update the var directly
UseState re renders the component through that it updates the dom
State is the brain of react (means when the components is re rendered it keeps the
other componenets data in track )
6.
In forms we should write onsubmit (not on button on click (f handleSubmit)) in
form element because when there is text written or something is selected
in form by pressing enter it is submiited directly on the other hand on button we
should have to click on that button
Render Phase Details:At the begining of the re rendering phase react will go
through the component tree and re render all the instances that triggeredd
the re render and react will render them this together will
create react elements which then will create the virtual dom
when the render happens on the components this will create React
Element tree which is called virtual dom
Virtual Dom : Tree of all react elements created from all instances in the
componenet Tree(it is a javascript object fast to create multiples)
When a component is rendered all of its childs components will be
rendered as well (example if the app component is
updated then it will re render the entire application this is
because that react dont know that this update will effect the
child or not(not the entire dom is updated but only the virtual
dom)) after this the virtual dom will reconciled(reconcilation + diff)
Reconsiler(engine :During the initial render of the application fiber takes the
entire element tree (virtual DOm) based on it create another
of react) tree which is called fiber tree
Important characteristic of fiber is that work can be done
asynchronusly(Rendering process can be split into chunks , tasks can be
prioritized over others and work can be paused reused or thrown
away if not valid anymore)
Fiber Tree :Fiber tree is the internal tree that has a fiber for each
component instance and dom elements(button etc)
Fiber are not created on every re render(instead it is a mutable
data structure and is mutated in future reconsil.. steps)
And this makes the fiber the perfect place for keeping tracks of
things like the current Component state , props ,side effects
etc ***The Actuall state and props of any component instance that
we see on the screen are internally stored inside the fiber tree
Also known as unit of work because it keeps queue of work (in this
tree each first child has a link to its parent and all the
other children then have a link to their previous siblings this
structure is called linked list )
the virtual dom and fiber tree not only has the structure of the
components but the entire dom structure
After all this process when the fiber tree is updated the list of
dom updates will be created
List of DOM Updates:React writes to the dom insertion deletion and updates (list of
dom updates are flushed to the dom)
key Prop :By Using the key prop when each render happens then react will
see it as a different component therefore it will change the state
:and not keep the state the same
State Update :Renders are not triggered immediatley but scheduled for when
the js engine has some "free time"
Batched There is batchin of multiple setState calls in event handlers
setTimeouts and now every where else
Means in one function there are multiple setState calls then it
exectues one state for the all
setStates in parrallel (how ever the state is not updated directly
but is happened after the re render)
in the below example the first three will update only one because in
the state is not updated with the rendering process
but after the re rendering in first iteration likes 0 + 1 how ever
the state is not updated in rendering process
so in the other line again likes will be 0 + 1 which will plus only
one to the number
function handleTripleInc() {
// setLikes(likes + 1);
// setLikes(likes + 1);
// setLikes(likes + 1);
Event Propagation :Event Propagation has two phases Capturing phase and Bubbling
phase In capturing phase when event is fired on a button
And Delegation then the event object is created which moves through each
parent down the dom tree until it reaches the desired target
after then when it is reached the target then it bubbles up the tree
during bubbling phase the event is fired(used)
but when it is bubbled up if the same event is used by some other
element then it will also be fired over there
this some time causes unnecessary things are happening for that we
can pause the bubbling (through e.stopPropagation)
but this is not the good way(only use this if there is no other
solution)
if you want to capture the event in the capturing phase then just
use onClickCapture but you will not need that
just to keep in mind
Now when we attach handler on the button then react will attach the
handler not to the button but to the root the div with classname
root react will attach one event hanler per event type and it does
during the root note of the fiber tree during the render phase
(if we have multiple onClick event handlers then react will bundle
them all together in a one big onClick handler to the root
node of the fiber tree)
**********The Render phase is commited by the react library (that is why we import
react) does not touch the dom it does not know where the render
Result will go
**********The Commit phase is commited by the library React Dom (that is why we
import ReactDom )
**********The Browser Paint is done by the browsers itself
--Never call a component directly in a component because then react will not see it
as a instance therefore then it can not manage its states
and its states will lives in the parent component in which it is called
**************************************SECTION 12
***********************************************************************************
*********
USE-EFFECT :As We can not use side effects in the render logic there are two ways
of using side effects which is event handler function
and UseEffect hook sometimes we can not use effect in the event handler
because we want the side effect to automatically happen after
the render happens.This effect takes the second argument as dependency
array which takes props and states
Now when the props changes the effect is executed and is same for state
and the componenet is re rendered
if we specify empty array in the dependency argument then it will
mount/render on the initial Render
if we specify nothing then every time the render happens then it will
be executed (Means will be connected with all props states
With every thing etc)
UseEffect is rendered after the render commit and browser paint because
if it contains a code that has long running process
if it will be executed before browser paint then it will take more time
to paint the browser there is a effect that is executed
before the browser is painted
Layout Effect : this is the effect that is executed before the browser paint but we
will not have need of this
The below example is of cleanup func the return is the cleanup func
Now if we execute the below function then it will be log to the console
title we have said that it runs after the component has unmounted
then how it remember its title that is because of closure (the function
rembebers all the variables at the time and the place that the function
was created) you can also check the first situation by selecting one movie
and again another
useEffect(
function () {
if (!title) return;
document.title = `Movie | ${title}`;
return function () {
document.title = "usePopCorn";
console.log(`Clean up effec for movie ${title}`);
};
},
[title]
);
USE-EFFECT :when we do fetch request when we type someword in the searcbar then
the componenet is re rendered which makes on each alphabet a request
IN you can see in network tab (It has a minor issue which means that
when we search inception if the search of inc tookes long
FETCH then the other alphabets then the last will be our query seeted and
also it has another issue that many data will be downloaded)
which is bad for performance
catch (err) {
if (err.name !== "AbortError") {
setError(err.message);
}
Using Use Effect For keypress and also removing that event because if we not remove
it then many events will be attached to it which will cause it
slow down its performance (Means if we open 10 movies details then 10 same event
listeners will be attached to the document)
useEffect(
function () {
function callback(e) {
if (e.code === "Escape") {
onCloseMovie();
}
}
document.addEventListener("keydown", callback);
return function () {
document.removeEventListener("keydown", callback);
};
},
[onCloseMovie]
);