react4
react4
=================
A component is a building block of react application.
React components are used to split our UI into reusable independent pieces.
ex:
Header, Footer, Section, Form , Table , Navbar and etc.
React components are like javascript functions because they accept arbitary inputs
like props and returns react element to describe how element should look like.
2) Class component
1) Function component
---------------------
Function component is also known as stateless component.
Named function
--------------
function App()
{
return (
)
}
export default App;
Anonymous function
-----------------
const App = function(){
return (
)
}
export default App;
Arrow function
----------------
const App = ()=>
{
return (
)
}
export default App;
Project structure
------------------
myapp4
|
|---node_modules
|
|---public
|
|---index.html
|---manifest.json
|---favicon.ico
|
|-----src
|
|---index.js
|---App.js
|----package.json
|----README.md
step1:
-----
Create a react project i.e myapp4.
ex:
npx create-react-app myapp4
step2:
-----
Open the VSC editor.
ex:
code .
step3:
------
Switch to the project.
ex:
cd myapp4
step4:
-----
Now install web-vitals dependency using npm.
ex:
npm install web-vitals
step5:
------
Run the react application.
ex:
npm start
step6:
-----
Now create a function component inside App.js file.
App.js
-------
function App()
{
return (
<h1> Function component </h1>
)
}
export default App;
App.js
------
function App(props)
{
return (
<>
<h1>Roll No : {props.rollno} </h1>
<h1>Name : {props.name}</h1>
</>
)
}
export default App;
2) Class component
-------------------
A class component is also known as statefull component.
ex:
import {Component} from 'react';
class App extends Component
{
render()
{
return (
)
}
}
export default App;
Project structure
------------------
myapp5
|
|---node_modules
|
|---public
|
|---index.html
|---manifest.json
|---favicon.ico
|
|-----src
|
|---index.js
|---App.js
|----package.json
|----README.md
step1:
-----
Create a react project i.e myapp5.
ex:
npx create-react-app myapp5
step2:
-----
Open the VSC editor.
ex:
code .
step3:
------
Switch to the project.
ex:
cd myapp5
step4:
-----
Now install web-vitals dependency using npm.
ex:
npm install web-vitals
step5:
------
Run the react application.
ex:
npm start
step6:
-----
Now create a function component inside App.js file.
App.js
-------
import {Component} from 'react';
class App extends Component
{
render()
{
return(
<h1> Class Component </h1>
)
}
}
export default App;
App.js
------
import {Component} from 'react';
class App extends Component
{
render()
{
return(
<>
<h1>Roll No : {this.props.rollno}</h1>
<h1>Name : {this.props.name}</h1>
</>
)
}
}
export default App;
Composing component
====================
One component calls to another component such concept is called composing
component.
Project structure
------------------
myapp6
|
|---node_modules
|
|---public
|
|---index.html
|---manifest.json
|---favicon.ico
|
|-----src
|
|---index.js
|---App.js (parent component)
|---Student.js (child component)
|----package.json
|----README.md
step1:
-----
Create a react project i.e myapp6.
ex:
npx create-react-app myapp6
step2:
-----
Open the VSC editor.
ex:
code .
step3:
------
Switch to the project.
ex:
cd myapp6
step4:
-----
Now install web-vitals dependency using npm.
ex:
npm install web-vitals
step5:
------
Run the react application.
ex:
npm start
step6:
-----
Now create a Student component inside "src" folder.
Student.js
----------
function Student()
{
return (
<h1> Student Component </h1>
)
}
export default Student;
step7:
-----
Now call Student component inside "App.js" file.
App.js
-------
import Student from "./Student";
function App()
{
return(
<Student/>
)
}
export default App;
App.js
------
import Student from "./Student";
function App(props)
{
return(
<Student fname={props.name}/>
)
}
export default App;
Student.js
----------
function Student(props)
{
return (
<h1> Welcome : {props.fname} </h1>
)
}
export default Student;