React JS Slides
React JS Slides
React JS Slides
BY PRASHANT TOMER
What is React
oComponents everywhere
JavaScript and HTML in the same file
goto-
File-→ open-→ Folder, locate your project where
you have created in the previous steps and open
OR
Right click on app you created and open visual
studio
Anatomy of a React component
The component is just Inputs are passed through a a
function single argument called “Props”
The function
export default function MyComponent(props) { outputs HTML
return <div>Hello, world! My name is {props.name}</div>;
}
return (
<div className="App">
</div>
}
Internal Style
Conditions
Looping
ArrayOfObject(Export and import
Component)
Create new JS file with Name “ArrayOfObject” and write the below code:
How to render the ArrayOfObject
File in App.js
Go to App.js File
<ArrayOfObject /> // way to use the external file in App.js or inside any
other js file.
O/P:
Import File in React-2(Create
Template.js)
const Template = () => {
return (
<div>
<form>
<label>Enter Id:</label><input type='text' name='name'></input>
<br />
<label>Enter Password:</label> <input type='password' name='password'></input>
<br />
<input type='submit' value='Login'></input>
<input type='submit' value='Reset'></input>
</form>
</div>
)}; export default Template
;
Go to App.js
Import the Template.js in App.js as given below
<div style={mystyle}>
<br></br>
<h2>Login Form</h2>
<Template /> // this is the way to call the Template file functionality in App.js
</div>
JSX Fragments
When returning elements in JSX, you can only return one element at a time.
That element can have children, but you must ensure that you're only returning one element at a
time, or else you will get a syntax error.
React solves this by making you return a Fragment that wraps the elements that you want to
return.
So if you want to return the below HTML from a function:
<h2>Free delivery</h2>
function Welcome(props) {
return <h1>Hello, {props.name}</h1>;
}
This function is a valid React component because it accepts a single “props” (which
stands for properties) object argument with data and returns a React element. We call
such components “function components” because they are literally JavaScript
functions.
Multiple Components
The Component allows you to split your UI into independent, reusable pieces.
This lets you think about each piece of your UI in isolation, making it easier to debug.
A React Component is a template or a blueprint that lets you describe a section of the UI; for
example, <Footer></Footer> is a React Component that describes the footer.
Class Component:
return (
<div className="App">
</div>
);
EventHandling
Modify previous code to display
data on browser
Introduction to state in React
State allows us to manage changing data in an application. It's defined as an object where we
define key-value pairs specifying various data we want to track in the application.
• class-based component
• functional component
React Hooks
Hooks are the new feature introduced in the React 16.8 version. It allows you to
use state and other React features without writing a class.
Hooks are the functions which "hook into" React state and lifecycle features
from function components. It does not work inside classes.
If you write a function component, and then you want to add some state to it,
previously you do this by converting it to a class. But, now you can do it by using
a Hook inside the existing function component.
Create new JS File “UseHook”:
Again do the same process, go to App.js file and
the component would remain as it was when it was initially created in the DOM. As you might
imagine,
many of the components you write till need to be updated, whether via a change in state or props.
There’s one more phase a React component can go through: the error handling
phase. This occurs when your code doesn’t run or there’s a bug somewhere. Think
of it like an annual physical.
Implementation
React Refs
When to Use Refs
here are a few good use cases for refs:
•Managing focus, text selection, or media playback.
•Triggering imperative animations.
•Integrating with third-party DOM libraries.
•Only use with class not with function
O/P:
REST API call to node JS
Integrate REST API with React and call the node JS, we need to add some install and add some
package in our application:
1. to send data from react to node js through REST Api, we need Axios package, so add this in your
application
npm install axios
Add in application
import Axios from axios;
2. to receive data at node js end (server side) we express js (Remember in Node Js Code)
npm install express
And import in code : const express = require('express');
const app = express();
Client Side Code (ReactJS )
ServerSide(Node JS ) ReceiveData.js
To run the client and server
We need two Terminal one for client (react app) and another one for server (node app), now open
two terminal as shown in given below figure:
Client Side Testing through Console
Server Side Verification and get
msg on client
1.Do Modification in server side code(ReceiveData.js file)
Axios.post("http://localhost:3001/login",
email: this.email.current.value,
password: this.password.current.value
})
.then(resp => {
alert(resp.data.message);
})
Integration of
React+Express+Node+Mongodb(Must Installed)
Call new API call for insert the data into mongodb Database(inside sendData() function )
Axios.post("http://localhost:3001/insert",
email: this.email.current.value,
password: this.password.current.value
})
Server Side Code(ReceiveData.js)