React-1 PDF KRVSPM
React-1 PDF KRVSPM
Que What are the Challenges faced in the modern frontend and build a
webapp like linkedin
These are the major challenges:
Single Page Application (SPA) Challenges: Websites are now behaving more like
applications, where they don't reload the entire page but only specific parts of the UI.
Code Maintainability: As the web app becomes more complex, keeping the code
organized and easy to understand becomes challenging. Collaboration among
developers becomes important to ensure everyone is on the same page. Writing
clear and readable code, breaking down the code into smaller parts, and adding
comments help in maintaining the code. Additionally, testing the code regularly
ensures that new changes don't break existing features, making it easier to fix issues
and add new features in the future..
Solution
Take away : There were no solutions to effciently update DOM -> React solves this
problem
In this section we will understand what is the purpose of React
Instructor cue : write this question and give the answer yourself
What is React?
React: It has the algorithm which can efficiently tell you how to manipulate the UI .
This is more-or less only purpose of react
Instructor cue : Now ask the question ->
Here are different User intefaces and the library we need to use with react to do
the changes:
<!DOCTYPE html>
<html lang = "en">
<head>
<meta charset = "UTF-8">
<meta http-equiv = "X-UA-Compatible" content = "IE = edge">
<meta name = "viewport" content = "width = device-width, initial-scale
= 1.0">
<title>Document</title>
</head>
<body>
</body>
</html>
Add cdn link of react and reactDOM : you can choose to open the link and show
that react is nothing but js file
<head>
<!-- algorithm to efficiently manipulate your UI -->
<script src = "https://unpkg.com/react@18/umd/react.development.js"
crossorigin></script>
<!-- React DOM -> uses react to update the DOM-->
<script src = "https://unpkg.com/react-dom@18/umd/react-
dom.development.js" crossorigin></script>
</head>
Instructor cue: run the code in browser. It will give error , now ask the
students why are you getting the error .
Answer : no, if that is the case now we will need a translator that can convert this
code to valid JS code
solution: As in the above function Hello which is returning something which is neither
html and not the javascript. Hence we use the Bable transpiler to convert the code
of the one language to another. Bable is a javascript transpiler to one language to
another here it is JSX to JS
Instructor cue: here for further understanding you can take the learners to
babel playground and paste the above component that will convert this code
into js code
Adding babel
<!DOCTYPE html>
<html lang = "en">
<head>
<body>
<!-- the whole application lives inside-->
<div id = "root"></div>
<!-- we are telling that it is to be converted by babel from JSX to js
-->
<script type = "text/babel">
-------
react code
-------
</script>
</body>
</html>
<!DOCTYPE html>
<html lang = "en">
<head>
<meta charset = "UTF-8">
<meta http-equiv = "X-UA-Compatible" content = "IE = edge">
<meta name = "viewport" content = "width = device-width, initial-scale
= 1.0">
<title>Document</title>
<!-- algorithm to efficiently manipulate your UI -->
<script src = "https://unpkg.com/react@18/umd/react.development.js"
crossorigin></script>
<!-- React DOM -> uses react to update the DOM-->
<script src = "https://unpkg.com/react-dom@18/umd/react-
dom.development.js" crossorigin></script>
<!-- transpiler -> JSX -> into javascript -->
<script src = "https://unpkg.com/babel-standalone@6/babel.min.js">
</script>
</head>
<body>
<!-- the whole application lives inside-->
<div id = "root"></div>
function HELLO(obj) {
return <h1>Hello React </h1>;
}
ReactDOM.render(<HELLO></HELLO>, document.getElementById("root"));
</script>
</body>
</html>
What is JSX
JSX: JSX, short for JavaScript XML, is like HTML on steroids. It allows you to write
JavaScript logic directly inside HTML code.
Explain these three Take Aways
Props
In React, we use it to make websites interactive. Now, let's learn how we can make
components act differently by giving them information.
Anology :
In the LinkedIn example below, think of creating a comment list as similar to how
LinkedIn organizes information on its comments page. You can break down the
comment into smaller parts, just like LinkedIn has multiple components within a
comment. For example, a comment could contain components for a profile picture,
options menu (like three dots), and user information
Question to learner: Imagine you want to display the "HELLO" component five
times and the "BYE" component once.
Answer:
We will use component composition , which is a fancy way of saying we can combine
smaller components to make bigger ones.
Imagine you have two parts of your app: a "HELLO" part and a "BYE" part. If you
want to show "HELLO" five times and "BYE" once, you can combine them together
to make a bigger component.
For example, we can create a new component called HELLO PARENT. This
component will include five instances of the "HELLO" component and one instance
of the "BYE" component. This idea of putting smaller components together to make
a larger one is what we call component composition.
<body>
<!-- the whole application lives inside-->
<div id = "root"></div>
function HELLOPARENT() {
return <div>
<HELLO name = {"Rohan"} age = {10}></HELLO>
<HELLO name = {"Rajneesh"} age = {30}></HELLO>
<HELLO name = "Krishna" age = {40}></HELLO>
<HELLO ></HELLO>
<BYE></BYE>
</div>
}
ReactDOM.render(<HELLOPARENT></HELLOPARENT>,
document.getElementById("root"));
</script>
</body>