“epg>-Student Information....
Note that the function name's first
letter must be in capital
jabel> Roll no {student Roll}
‘
:
}
Passing the values to the
functional component
const element =
//render this component as Id='root’
ReactDOM.render(element,document.getElementByld(root);
Step 3 : Open the terminal window or command-prompt. Switch to funcomponent folder and
issue the command npm start. The web browser will get opened up automatically
and the output will be displayed as follow:
D React App = Oo x
G_—_O locathost:3000 8
Student Information...
Roll_no:111
+/o 0)
Name:Chitra
F ieee 234567890
; TECHNICAL PUBLICATIONS® - an up-thrust for knowiedge———————————
Full Stack Web Development
2, Class component
React.Cor
To define the class ‘component we have to first create & class and extend
class,
«elo is class component which
Class components are
6 classes that retum
to Welcome funetion(as given in Funetional component):
Class Welcome extends React. Component {
render() {
Tetum Welcome, {this props.name};
}
}
Note thatthe class component must have the render() method fr retuming ISX.
Demo Example
Step 1 Open the command prompt and issue the following command
‘Di\reactDemo> create-react-app classcomponent
By this command we ae erating a workspace by he name clascomponent in which
be creating our function component.
Step 2: Locate index,s ile inthe sre folder of you application and edit it as follows -
index,js
impor React from react!
impor ReactDOM fom teact-domt
impor 'Jindex.css;
class Student extends React Component{
render(){
retum
« Student Information. .
}
} :
const element =
Explain the class component in React.
3 ven the ants.
3. Given the difference berween functional and class components
EX] Inter Components Communication
React is a component-based UI library, When the Ul is split into small, focused components,
they can do one job and do it swell. But in order to build up a system into something that can
accomplish an interesting task, multiple components are needed. These components often need to
work in coordination together and, thus, must be able to communicate with each other. Data must
be exchanged among the components
In this section we will discuss the communication from parent class to child class.
Step 1
Go to the directory in which the first application is to be created. At the command
prompt issue the command create-react-app
Step 2: Now open the corresponding folder and edit the index.js file as follows
index.js
import React from ‘react’;
import ReactDOM from ‘react-dom'
class Student extends React.Component{
constructor(props){
super(props);
}
render(){
retum
Student Information....
Roll_no: {this props Roll}
Name: {this props.Name}
phone: {this.props.Phone)}
Total Marks: {this props Marks}
}
t
{this.props.Maths}
TECHNICAL PUBLICATIONS® - an up-thnust for knowledgo-
stack Web Development 5-11 Resch
| pul
gjans Marks extends React.Component(
‘onstructor(Props){
super(Props):
render(){
retum
Welcome {name}
)
}
3
ReactDOM.ender{ document.getElementByld(‘roct’))
anotherModule.js
const name = ‘Parth’
export default name;
mystyle.css
body {
background: khaki;
i
hi{
color-red;
+
Reac
TECHNICAL PUBLIGATIONS®. an up or krowlodge_—
Web Development: 5-31 React
ul St00k
index.htm!
ZipOCTYPE htrnl>
entml>
‘ctitle>Modularization and WebPack Demo
‘
yt
export default Welcome
Step 4 : Again create one more js file inside the sre folder. 1 have named it as
contact.js(Again note that c is in small letter here.)
contact.js
import React from ‘rea
function Contact(){
Teturn(
‘
Full Stack Web Development 5-34 React
Step
Locate index,js from sre folder. Specify the routes for the above created two files
welcome,js and contaet,js files.
index,js
import React from ‘react’;
import ReactDOM from 'react-dom';
import {BrowserRouter, Link, Switch Route,Routes} from ‘react-router-dor
import Welcome from ‘welcome’:
import Contact from '/contact’;
function App(){
‘
;
}
Altematively the functional component can be created using arrow function as follows -
const Welcome = (props) => {
retum
Welcome {props.name}1>:
}
Q.4 How to define class components in ReactJs ?
Ans.: To define the class component we have to first create a class and extend
React. Component class.
Class components are ESG classes that return JSX. Below, is a class component which is
similar to Welcome function(as given in Functional component) :
class Welcome extends React.Component {
render() {
retum