A very great feature in optional chaining.
Naturally, it is for objects. Check if this property exists or not.
But there is something that will benefit you.
It works with function as well
Like this
callback?.()
If this callback function exists? It will be implemented.
If it is not there. Nothing will happen
Here you will need it when you create a custom hook and pass a function to it as another parameter
and take a look at #rules_of_hooks
https://react.dev/reference/rules/rules-of-hooks
Naturally, it is for objects. Check if this property exists or not.
But there is something that will benefit you.
It works with function as well
Like this
callback?.()
If this callback function exists? It will be implemented.
If it is not there. Nothing will happen
Here you will need it when you create a custom hook and pass a function to it as another parameter
and take a look at #rules_of_hooks
https://react.dev/reference/rules/rules-of-hooks
π₯3β€1
overreacted.io
A blog by Dan Abramov
π₯1π₯°1
https://blog.isquaredsoftware.com/2020/05/blogged-answers-a-mostly-complete-guide-to-react-rendering-behavior/?ref=jonas.io
Complete Guide to React Rendering Behavior
Complete Guide to React Rendering Behavior
Mark's Dev Blog
Blogged Answers: A (Mostly) Complete Guide to React Rendering Behavior
Details on how React rendering behaves, and how use of Context and React-Redux affect rendering
https://angularindepth.com/posts/1008/inside-fiber-in-depth-overview-of-the-new-reconciliation-algorithm-in-react
Inside Fiber: in-depth overview of the new reconciliation algorithm in React
Inside Fiber: in-depth overview of the new reconciliation algorithm in React
Angular.love
Reconciliation algorithm in React - what is it and how it works
Angular.love - a place for all Angular enthusiasts created to inspire and educate.
π₯2
https://thoughtspile.github.io/2021/05/17/everything-about-react-refs/
Everything about React refs
What you should and shouldn't do with useRef
Everything about React refs
What you should and shouldn't do with useRef
Vladimir Klepov as a Coder
So you think you know everything about React refs
https://react.dev/reference/react/useSyncExternalStore
https://twitter.com/sebastienlorber/status/1615329010761842689
useSyncExternalStore β‘οΈ good way to prevent React hydration issues
βAnd take a look at thisβοΈ
https://julesblom.com/writing/usesyncexternalstore?ref=jonas.io
https://twitter.com/sebastienlorber/status/1615329010761842689
useSyncExternalStore β‘οΈ good way to prevent React hydration issues
βAnd take a look at thisβοΈ
https://julesblom.com/writing/usesyncexternalstore?ref=jonas.io
π₯°2π₯1
https://alexsidorenko.com/blog/react-render-always-rerenders/
A Visual Guide to React Rendering - It Always Re-renders
A Visual Guide to React Rendering - It Always Re-renders
Alex Sidorenko
A Visual Guide to React Rendering - It Always Re-renders | Alex Sidorenko
Contrary to popular belief, by default, react component doesn't care whether its props changed or not. It will always re-render when its parent renders.
βοΈHydration in ReactβοΈ
https://medium.com/@gautamkiran123/hydration-in-react-8e8dff384f93
From here. You will know the difference between CSR and SSR more clearly.
Then we will know what Hydration is.
If HTML content is not rendered beforehand you canβt use
https://medium.com/@gautamkiran123/hydration-in-react-8e8dff384f93
From here. You will know the difference between CSR and SSR more clearly.
Then we will know what Hydration is.
hydrateRoot() API
will compare the rendered content and the server-rendered content and will warn you in case of any mismatches during hydration.If HTML content is not rendered beforehand you canβt use
hydrateRoot()
.In this case , createRoot()
has to be used.createRoot()
returns an object with two methods β render
and unmount
root.unmount()
is mostly useful when the rootβs DOM node or any of its ancestors gets removed, we can stop removing everything inside that is related to it by calling the unmount
method. In simple words, we are telling React to stop with removing the React nodes after we have removed what was necessary , by calling unmount()
method.Medium
hydration in React
diving deep into the concept of hydration
π₯°2π₯1
βοΈESlint configuration at React with viteβοΈ
When initialize React app with vite
don't forget configure Eslint
And create new file at globally
This file to extend the default rules of ESlint with React rules.
The rules will be like this:
Then, in
import eslint like this:
And add eslint() function inside plugins like this:
After that, ESlint will work at your app (β βΏβ )
When initialize React app with vite
npm create vite@latest
don't forget configure Eslint
npm install eslint vite-plugin-eslint eslint-config-react-app --save-dev
And create new file at globally
.eslintrc.json
This file to extend the default rules of ESlint with React rules.
The rules will be like this:
{
"extends": "react-app"
}
Then, in
vite.config.js
fileimport eslint like this:
import eslint from "vite-plugin-eslint"
And add eslint() function inside plugins like this:
plugins: [react(), eslint()],
After that, ESlint will work at your app (β βΏβ )
π₯°4π2β€1
βοΈNested Routes and Index RouteβοΈ
with react-router-dom
Child Route like this:
The path: "/app/cities"
"/app" β Parent Route
"/citites" β Child Route
Nested Routes like this:
So, <AppLayout /> is the element that's rendered in "/app" any components are exist in <Applayout />
We can use
Then, The elements at Child Routes will rendered at any place in "/app" by instead of <Outlet />
βοΈindex RouteβοΈ
like this:
index={true} is same path={"/"}
or same path of Parent Route
with react-router-dom
Child Route like this:
<Route path={"/app/cities"} element={<CityList />}/>
The path: "/app/cities"
"/app" β Parent Route
"/citites" β Child Route
Nested Routes like this:
<Route path={"/app"} element={<AppLayout/>}>
// Child Routes here
</Route>
So, <AppLayout /> is the element that's rendered in "/app" any components are exist in <Applayout />
We can use
<Outlet />
in any components in AppLayout<Outlet />
this to render elements that in Child Routes Then, The elements at Child Routes will rendered at any place in "/app" by instead of <Outlet />
βοΈindex RouteβοΈ
like this:
<Route index={true} element={<Cities />}/>
index={true} is same path={"/"}
or same path of Parent Route
π₯°2π1
What happen when enter wrong Route in URL ??
- We will add new Route in globally, his path will be "*"
like this:
this Route will be the last Route inside <Routes> ... </Routes>
- We will add new Route in globally, his path will be "*"
like this:
<Route path={"*"} element={<PageNotFound/>}/>
this Route will be the last Route inside <Routes> ... </Routes>
π₯1π₯°1
βοΈStoring state in the URLβοΈ
URL is the best place to store UI state and alternative to useState
(example): open or close panels - currently selected list item - list sorting order - applied list filter
- This useful to make bookmark and share the page
URL like this:
/app/cities β Path
/lisbon β params
?lat=38.1242&lang=-9.8364 β query string
params β very useful to pass data to the next page
query string β useful to store some global state
I recommend this topic that about DNS:
https://maryam-eid.medium.com/decoding-the-web-iv-dns-domain-names-9e0c5b3a0f27
URL is the best place to store UI state and alternative to useState
(example): open or close panels - currently selected list item - list sorting order - applied list filter
- This useful to make bookmark and share the page
URL like this:
localhost:5173/app/cities/lisbon?lat=38.1242&lang=-9.8364
/app/cities β Path
/lisbon β params
?lat=38.1242&lang=-9.8364 β query string
params β very useful to pass data to the next page
query string β useful to store some global state
I recommend this topic that about DNS:
https://maryam-eid.medium.com/decoding-the-web-iv-dns-domain-names-9e0c5b3a0f27
β€1π₯1
βοΈSo to use Params with React RouterβοΈ
Step-1: Create a new Route
Step-2: Link to that Route (useParam)
Step-3: In that Route, we read the state from URL
1-
Route example:
here "83723" is "id" param
and "madrid" is "cityname" param
2-
here must use template literals with params
3-
Then to get that data from URL.
We use
We will use this hook only at component in the Route have param like this in step-1 in <City /> component.
to install React Router (Β¬βΏΒ¬)
Step-1: Create a new Route
Step-2: Link to that Route (useParam)
Step-3: In that Route, we read the state from URL
1-
<Route path={"/app/cities/:id/:cityname"} element={<City/>}/>
Route example:
localhost:5173/app/cities/83723/madrid
here "83723" is "id" param
and "madrid" is "cityname" param
2-
<Link to={`/app/cities/${ele.id}/${ele.cityName}`}> ...... </Link>
here must use template literals with params
3-
Then to get that data from URL.
We use
useParams()
hook.We will use this hook only at component in the Route have param like this in step-1 in <City /> component.
import {useParams} from "react-router-dom";
const {id, cityname} = useParams();
useParams()
provided by React Routerto install React Router (Β¬βΏΒ¬)
npm install react-router-dom@version
β€3
βοΈIn Express.jsβοΈ
XSS-attack in Back-end.
This the best way to solve.
You can use
to install xss library:
example:
XSS-attack in Back-end.
This the best way to solve.
You can use
xss library
and this middleware instead.to install xss library:
npm install xss
// 1. turning object into a big string using JSON.stringify
// 2. sanitize it using xss
// 3. parsing it after sanitization
app.use((req, res, next) => {
const sanitizeBody = xss(JSON.stringify(req.body));
req.body = JSON.parse(sanitizeBody);
// π«‘dont forget to add req.params req.query and ....
next();
});
example:
// input:β οΈ
{
test: [
{
t: "this is my .md content. <strong>yo<strong> <script>alert('xss');</script> ",
a: "<script>alert('xss');</script>",
ts: "<script>alert('xss');</script>",
testing: [Array]
}
]
}
// result:π§Ό
{
t: "this is my .md content. <strong>yo<strong> <script>alert('xss');</script> ",
a: "<script>alert('xss');</script>",
ts: "<script>alert('xss');</script>",
testing: [ { now: "<script>alert('xss');</script>" } ]
}
π1π₯1π₯°1