React JS - Part II
React JS - Part II
TECHNOLOGY
What is React?
2. Then the difference between the previous DOM representation and the new
one is calculated.
3. Once the calculations are done, the real DOM will be updated with
only the things that have actually changed.
Differentiate between Real DOM and Virtual
DOM.
■ If you have npx and Node.js installed, you can create a React application by using create-react-
app.
■ Now you are ready to run your first real React application!
■ Run this command to move to the my-react-app directory:
cd my-react-app
■ Run this command to run the React application my-react-app:
npm start
■ A new browser window will pop up with your newly created React App! If
not, open your browser and type localhost:3000 in the address bar.
■ The result:
React ES6
■ What is ES6?
■ ES6 stands for ECMAScript 6.
■ ECMAScript was created to standardize JavaScript, and ES6 is the 6th
version of ECMAScript, it was published in 2015, and is also known as
ECMAScript 2015.
React ES6 Classes
■ ES6 introduced classes.
■ We use the keyword class, and the properties are assigned inside a
constructor() method.
■ Example
■ A simple class constructor:
class Car {
constructor(name) {
this.brand = name;
}
}
■ Create an object called "mycar" based on the Car class:
class Car {
constructor(name) {
this.brand = name;
}
}
Before:
hello = function() {
return "Hello World!";
}
■ In fact, if you have only one parameter, you can skip the parentheses
as well:
■ Arrow Function Without Parentheses:
■ hello = val => "Hello " + val;
What is an arrow function and how is it
used in React?
■ If you use let inside of a block, i.e. a for loop, the variable is only
available inside of that loop.
const
3) const x = 5.6;
■ const is a variable that once it has been created, its value can never change.
■ ReactDOM.render(<p>Hello</p>, document.getElementById('root’));
■ Example:
■ Execute the expression 5 + 5:
■ const myElement = <h1>React is {5 + 5} times better with
JSX</h1>;
Can web browsers read JSX directly?
■ Web browsers cannot read JSX directly. This is because they are built
to only read regular JS objects and JSX is not a regular JavaScript object
■ For a web browser to read a JSX file, the file needs to be transformed
into a regular JavaScript object. For this, we use Babel
Why can’t browsers read
JSX?
■ Browsers can only read JavaScript objects but JSX in not a regular
JavaScript object.
■ Thus to enable a browser to read JSX, first, we need to transform
JSX file into a JavaScript object using JSX transformers like Babel and
then pass it to the browser.
function Greeting(props) {
return <h1>Welcome to {props.name}</h1>;
}
Class Components:
■ These types of components can hold and manage their own state and
have a separate render method to return JSX on the screen.
■ They are also called Stateful components as they can have a state.
■ When a value in the state object changes, the component will re-
render, meaning that the output will change according to the new
value(s).
Add a button with an onClick
event that will change the color
property:
class Car extends render() {
React.Component return (
<div>
{ constructor(props) { <h1>My {this.state.brand}</h1>
super(props); <p>
this.state = { brand: "Ford", It is a {this.state.color}
model: "Mustang", color: "red", {this.state.model}
from {this.state.year}.
year: 1964 }; </p>
} changeColor = () => <button type="button“
{ this.setState({color: onClick={this.changeColor} >
"blue"}); } Change color</button>
</div>
);
}
}
Lifecycle of Components
initial values. ■ }
■ render() {
■ The constructor() method is
called with the props, as ■ return (
Allows to pass
data from one
Holds information about component to
Use
the components other
components as
an argument
■ React just like a lot of things has disadvantages. Some of them are:
■ States are the heart of React components. States are the source of
data and must be kept as simple as possible. Basically, states are the
objects which determine components rendering and behavior. They
are mutable unlike the props and create dynamic and interactive
components. They are accessed via this.state().
Differentiate between states and
props.
■ Events are named using camel case instead of just using the lowercase.
■ Events are passed as functions instead of strings.
■ The event argument contains a set of properties, which are specific to
an event. Each event type contains its own properties and behavior
which can be accessed via its event handler only.
Styling React Using CSS
■ There are many ways to style React with CSS, this tutorial will take a
closer look at three common ways:
• Inline styling
• CSS stylesheets
• CSS Modules
Inline Styling
■ To style an element with the inline style attribute, the value must be a JavaScript object:
■ Example:
■ Insert an object with the styling information:
■ const Header = () => {
■ return (
■ <>
■ <h1 style={{color: "red"}}>Hello Style!</h1>
■ <p>Add a little style!</p>
■ </>
■ );
■ }
Example:
■ First of all, we can not connect React JS to MongoDB because things don’t work like this. First, we
create a react app, and then for backend maintenance, we create API in node.js and express.js which is
running at a different port and our react app running at a different port. for connecting React to the
database (MongoDB) we integrate through API. Now see how we create a simple React app that takes
the input name and email from users and saved it to the database.
■ Prerequisite:
■ NodeJS installed in your system (install)
■ MongoDB installed in your system (install)
■ Setum Files and Folders: Setting up the required files and folders for the frontend and backend both
one by one.
■ Create React App: To build a react application follow the below steps:
■ Step 1: Create a react application using the following command
■ npx create-react-app foldername
■ Step 2: Once it is done change your directory to the newly created application using the
following command
■ cd foldername
■ Step to run the application: Enter the following command to run the application.
■ npm start
■ Backend Setup With NodeJS: Setup NodeJs for Backend to integrate with frontend.
■ Step1: Make a folder in the root directory using the following command
■ mkdir backend
■ Step 2: Once it is done change your directory to the newly created folder called backend
using the following command
■ cd backend
■ Step 3: Run the following command to create configure file
■ npm init -y
■ Step 3: Now Install the mongoose MongoDB using the
following command.
■ touch index.js
■ Project Structure: It will look like the following:Step to run the
application: Enter the following command to run the
application.
■ nodemon index.js