FEWD Unit-2 ReactJS
FEWD Unit-2 ReactJS
Prepared By
C Vikas
ReactJS
Unit - 2
Objectives
• This Course will help the student to comprehend and apply the concepts of
HTML,CSS and Bootstrap
• The student would be able to gain knowledge and implement the component-
based architecture
• As a result, from the user perspective, we get an almost immediate reaction to any
manipulation
React Components
• React allows us to create reusable UI components.
• A component is nothing but a piece of code/UI which is independent, isolated and
reusable
With these components we build complex user interfaces in React
• Every react application has one main component which is the root component
which represents the tree of components.
So, if we want to build an app we can split the page into many components (like
news, weather, …)
Something like this
Making of components or elements- Render method
Some
change
occurred
We simply change the state of the component and REACT will update the DOM
So, basically react , reacts to state changes.
Rendering HTML in React
• We use ReactDOM to render HTML in React.
• ReactDOM.render() function (usually) takes two arguments, HTML code and an
HTML element.
Let us create few elements using React
Even though all of this could easily fit on one line, it's generally a good idea to add line breaks to your
arguments for ReactDOM.render()
Now that we know how to render stuff, let's make our
app a little more complex by introducing child elements
Adding a child element
Let's add a sibling. We'll create a paragraph element and then pass it as another child to our
container children.
The code now is becoming a little more complex.
But then to create each element the way we did, is a hassle.
Select , react and type JSX text in left box, you will see react text
on right
React – process flow
• Babel tool converts JSX into React and ReactDOM library converts React JS to html for the browser
to render.
• Notice the difference in jsx and html – className and class, input element too is different.
• This is the main feature of React but this is not the essence of it. There is more to react.
comparison
If the sum element had any children they would be passed as the 3rd parameter
Sum element wrapped in H1 element
HTML elements are passed as strings (‘h1’)
H1 element has no
Parameters/attributes so
null is being passed.
But how do we get Babel to work for our code.
Where do we add the compiler
We add it as a library using the script tag
And how shall we say that a piece of code needs to be
converted from JSX to JS?
• Instead of the regular script tag add type attribute
Now that we know how JSX works, let us look at
an example
Is the below code ReactJS or JSX?
This does look like a lot of code when we compare it with vanilla java script. But then
the advantage that we get is far better!
What advantage?
• At first glance both implementations seem same
The advantage!
• For this page, check tools- elements tab in the browser.
• You will see that the dateJS div is updated every second in the browser
• Where as the reactJS is not updated every second
• Also, the other big advantage, try typing something in the input field of the JS
version, we will not be able to as the div keeps refreshing!
• Whereas in the react version we will be able to type without any problem, even
though we asked react to update every second
• React is smart enough to update only what’s necessary!
The Element tree
• React does not use html directly, it uses a tree of JS
objects to define the UI.
• We are writing HTML in JS! Unlike how we are used to
writing since ages, in HTML page!
The policy of react
• Policy of React, one way binding, is what is different about React when compared
to other libraries
• There are some libraries out there that use 2 way binding, but React simply renders
the UI from the object tree every time it needs updating. This smart way is called
Tree reconciliation.
• As only p element is changed React instructs the renderer to redraw only that
element. Once this is done, react disposes the old tree
References
• https://react.dev/
• https://www.w3schools.com/REACT/DEFAULT.ASP
• https://legacy.reactjs.org/docs/getting-started.html