ReactJS Developer Guide
ReactJS Developer Guide
React Js
Syllabus:
➢ Components
➢ Multiple Components
➢ State&Props
➢ Events&refs
➢ Pure Components
➢ Life cycle Hooks
➢ Unit Test Cases
➢ Introducton to Redux
➢ Multiple Reducers
➢ Integration of react and redux
➢ Saga Middleware
➢ Thunk Middleware
➢ Single Page Applications
➢ Lazy Loading
➢ Routing Parameters in SPA
➢ Crud Operations(MYSQL)
➢ Mini Project(MYSQL-MSSQL-MONGODB)
➢ Crud Operations(Thunk-Middleware)
➢ Mini project(Saga-Middleware)
➢ Server Files(MYSQL-MSSQL-MONGODB)
React Js
Commands
Create-react-app <application-name>
➢ Switch to project
Cd <project-name>
yarn start
Introduction
website : [Link]
file : [Link]
React Js
Ex. Demo
> cd first-app
Directory Structure
node_modules
public/[Link]
public/[Link]
o /[Link]
o /[Link]
React Js
public/[Link]
public/[Link]
src
src/[Link]
[Link]
[Link]
src/[Link]
src/[Link]
src/[Link]
[Link]
Ex.
redux
redux-thunk
redux-saga
axios
[Link]
Chapter-1(Components)
➢ Simple "JSX Class" behaves like Component.
➢ React Applications are Component Based Applications.
➢ we can create more than one Component.
➢ These Components are Reusable.
➢ Because of Components, React Simplifies the Complex UI.
Stateful Components
> cd first-app
<script
src="[Link]
crossorigin></script>
<script src="[Link]
[Link]" crossorigin></script>
<script src="[Link]
[Link]" crossorigin></script>
<link rel="stylesheet"
href="[Link]
[Link]"integrity="sha384-
9aIt2nRpC12Uk9gS9baDl411NQApFmC26EwAOH8WgZl5MYYxFfc+NcPb1dKGj7Sk
"crossorigin="anonymous"/>
Directory Structure
********************************************************
first-app
src
[Link]
[Link]
*******************************************************
React Js
[Link]
render(){
return(
<div>
<Container fluid>
{/*
{['primary','info','danger','success','dark'].map((element,index
)=>(
React Js
<Alert variant={element}>Alert</Alert>
))} */}
<Alert variant={'danger'}>
<[Link]>Heading</[Link]>
<Button>Learn More</Button>
</Alert> </Container>
</div>
) }};
[Link]
import './[Link]';
[Link](
<First />,
[Link]('root'));
React Js
[Link]();
******************************************
Stateless Components
> cd first-app
Directory Structure
********************************************************
first-app
src
[Link]
[Link]
[Link]
*******************************************************
React Js
[Link]
render(){
return(
<div>
</div>
};
};
[Link]
};
[Link]
import './[Link]';
[Link](
<[Link]>
<App />
</[Link]>,
[Link]('root')
);
// If you want your app to work offline and load faster, you can
change
Chapter-2(Multiple-Components)
> cd demo-app
"[Link]
[Link]
<link rel="stylesheet"
href="[Link]
,700&display=swap" />
React Js
<link rel="stylesheet"
href="[Link]
/>
<script src="[Link]
ui/core@latest/umd/[Link]"></script>
<script src="[Link]
ui/core@latest/umd/[Link]"></script>
************************************************
demo-app
src
[Link]
[Link]
[Link]
[Link]
[Link]
*************************************************
[Link]
render(){
return(
<div>
</div>
};
[Link]
render(){
return(
<div>
<Typography variant="h5">Deno</Typography>
</div>
} };
React Js
[Link]
render(){
return(
<div>
<Typography variant="h5">Azure</Typography>
</div>
};
[Link]
render(){
return(
<div>
React Js
<FrontEnd />
<Server />
<BackEnd />
</div>
};
[Link]
import './[Link]';
[Link](
<MernComponent />,
[Link]('root')
);
// If you want your app to work offline and load faster, you can
change
React Js
[Link]();
***************************************
React Js
> cd data-app
Directory Structure
********************************************************
data-app
src
[Link]
[Link]
[Link]
[Link]
*******************************************************
React Js
[Link]
constructor(){
super();
[Link] = {
render(){
return(
<div>
<Child key1={[Link]}/>
</div>
};
[Link]
constructor(){
super();
[Link] = {
render(){
return(
<div>
<Subchild {...[Link]}
key2={[Link]}/>
</div>
};
[Link]
render(){
return(
<div>
React Js
<h1>{[Link].key1}......{[Link].key2}</h1>
</div>
}}
[Link]
import './[Link]';
[Link](
<Parent />,
[Link]('root')
);
// If you want your app to work offline and load faster, you can
change
[Link]();
***********************************
React Js
> cd get-ex
Directory Structure
************************************************
Get-ex
src
[Link]
[Link]
*************************************************
React Js
[Link]
constructor(){
super();
[Link] = {
arr : []
};
componentDidMount(){
[Link]("[Link]
.then((posRes)=>{
[Link]({
arr : [Link]
})
},(errRes)=>{
[Link](errRes);
});
};
React Js
render(){
return(
<div>
<table border="1"
cellPadding="10px"
cellSpacing="10px"
align="center">
<thead style={{backgroundColor:"grey"}}>
<tr>
<th>SNO</th>
<th>Name</th>
<th>Capital</th>
<th>NativeName</th>
<th>Population</th>
<th>Flag</th>
</tr>
</thead>
<tbody>
{[Link]((element,index)=>(
<tr key={index}>
<td>{index+1}</td>
<td>{[Link]}</td>
React Js
<td>{[Link]}</td>
<td>{[Link]}</td>
<td>{[Link]}</td>
<td><img width="100px"
height="50px" src={[Link]}></img></td>
</tr>
))}
</tbody>
</table>
</div>
};
[Link]
import './[Link]';
[Link](
React Js
<Countries />,
[Link]('root')
);
// If you want your app to work offline and load faster, you can
change
[Link]();
React Js
Chapter-3(State&Props)
> cd first-app
Directory Structure
********************************************************
first-app
src
[Link]
[Link]
*******************************************************
[Link]
}
}
render(){
return(
<div>
<table border="1"
cellPadding="10px"
cellSpacing="10px"
align="center">
<thead style={{backgroundColor:"grey"}}>
<tr>
<th>SNO</th>
<th>PID</th>
<th>PNAME</th>
<th>PCOST</th>
React Js
</tr>
</thead>
<tbody>
{[Link]((element,index)=>
(
<tr>
<td>{index+1}</td>
<td>{element.p_id}</td>
<td>{element.p_name}</td>
<td>{element.p_cost}</td>
</tr>
))}
</tbody>
</table>
<h1>{[Link]([Link])}</h1>
<h1>{[Link]}</h1>
<h1>{[Link]}</h1>
</div>
)
}
}
[Link]
import './[Link]';
[Link](
<App />,
[Link]('root')
);
// If you want your app to work offline and load faster, you can
change
[Link]();
****************************************
setState()
> cd first-app
React Js
Directory Structure
********************************************************
first-app
src
[Link]
[Link]
*******************************************************
[Link]
};
myFun = ()=>{
[Link]({
sub : "Angular10"
});
};
};
[Link]
import './[Link]';
[Link](
<App />,
[Link]('root')
);
// If you want your app to work offline and load faster, you can
change
[Link]();
********************************************
Props:
> cd first-app
Directory Structure
React Js
********************************************************
first-app
src
[Link]
[Link]
[Link]
*******************************************************
[Link]-:
Code:
constructor(){
super();
[Link] = {
"data" : "React",
"version" : 16.8,
React Js
"flag" : true,
"obj" : {"cost":100000},
"arr" : [{"eno":111,"ename":"e_one","esal":10000},
{"eno":222,"ename":"e_two","esal":20000},
{"eno":333,"ename":"e_three","esal":30000},
{"eno":444,"ename":"e_four","esal":40000},
{"eno":555,"ename":"e_five","esal":50000}]
};
render(){
return(
<div>
<Second key1={[Link]}
key2={[Link]}
key3={[Link]}
key4={[Link]}
key5={[Link]}/>
</div>
};
};
React Js
[Link]-:
Code:
render(){
return(
<div>
<table border="1"
cellPadding="10px"
cellSpacing="10px"
align="center">
<thead style={{backgroundColor:"grey"}}>
<tr>
<th>SNO</th>
<th>ENO</th>
<th>ENAME</th>
<th>ESAL</th>
</tr>
React Js
</thead>
<tbody>
{[Link]((element,index)=>(
<tr>
<td>{index+1}</td>
<td>{[Link]}</td>
<td>{[Link]}</td>
<td>{[Link]}</td>
</tr>
))}
</tbody>
</table>
<h1>{[Link]([Link].key4)}</h1>
<h1>{[Link]([Link].key3)}</h1>
<h1>{[Link].key2}</h1>
<h1>{[Link].key1}</h1>
</div>
};
[Link]-:
React Js
import './[Link]';
[Link](
<[Link]>
<First />
</[Link]>,
[Link]('root')
);
[Link]();
React Js
OutPut:
state props
1. State is variables, but directly initialized 1. Props are owned by a parent
and managed by the component. component
2. A state is a variable which exists inside 2. props are read-only in the child
a component, that cannot be accessed component that receives from
and modified outside the component parent component. However, call
and can only be used inside the back functions can also be
component. Works very similarly to a passed, which can be executed
variable that is declared inside a inside the child to initiate an
function that cannot be accessed update.
outside the scope of the function in 3. props are immutable (unable to be
normal Javascript. changed).
3. The component itself will update the 4. Props” is a special keyword
state using the setState() function. in React, which stands for
4. States are declared inside the properties and is being used for
constructor via super() method. passing data from one component
to another.
React Js
Chapter-4(Events&Refs)
Events:
> cd events-app
Directory Structure
********************************************************
events-app
src
[Link]
[Link]
*******************************************************
[Link]
// fun_one = ()=>{
// };
// render(){
// return(
// <div>
// <button
onClick={this.fun_one}>ClickMe</button>
// </div>
// )
// }
// };
// fun_one = (arg1,arg2)=>{
// alert("Login Success");
// }else{
// alert("Login Fail");
// }
// };
// render(){
React Js
// return(
// <div>
// </div>
// )
// };
// };
[Link]
[Link](
<Events />,
[Link]('root')
);
// If you want your app to work offline and load faster, you can
change
// unregister() to register() below. Note this comes with some
pitfalls.
// Learn more about service workers: [Link]
[Link]();
React Js
**********************************************
Refs:
> cd events-app
Directory Structure
********************************************************
events-app
src
[Link]
[Link]
*******************************************************
[Link]
constructor(){
super();
[Link] = {
login : ""
};
};
render(){
return(
<div>
<fieldset>
<legend>Login Form</legend>
<input type="text"
ref="uname"
placeholder="User Name"></input>
<br></br><br></br>
<input type="password"
ref="upwd"
placeholder="User Password"></input>
React Js
<br></br><br></br>
<button onClick={[Link]}>Login</button>
<br></br><br></br>
<h1>{[Link]}</h1>
</fieldset>
</div>
login = ()=>{
[Link]({
login : "success"
})
}else{
[Link]({
login : "fail"
})
}
React Js
};
[Link]
[Link](
<Events />,
[Link]('root')
);
// If you want your app to work offline and load faster, you can
change
// unregister() to register() below. Note this comes with some
pitfalls.
// Learn more about service workers: [Link]
[Link]();
*******************************************************
> cd events-app
Directory Structure
********************************************************
events-app
src
[Link]
[Link]
*******************************************************
[Link]
constructor(){
super();
[Link] = {
res : {}
};
render(){
return(
React Js
<div>
<input type="text"
ref="msg"
placeholder="Enter Message"></input>
<button onClick={this.fun_one}>Change</button>
<h1>{[Link]([Link])}</h1>
</div>
};
fun_one = ()=>{
[Link]("[Link]
[Link]/test/uppercase",{"message":[Link]
lue})
.then((posRes)=>{
[Link]({
res : [Link]
})
},(errRes)=>{
[Link](errRes);
});
};
React Js
[Link]
[Link](
<Events />,
[Link]('root')
);
// If you want your app to work offline and load faster, you can
change
// unregister() to register() below. Note this comes with some
pitfalls.
// Learn more about service workers: [Link]
[Link]();
*******************************************
> cd demo-app
Directory Structure
********************************************************
demo-app
src
[Link]
[Link]
*******************************************************
[Link]
constructor(){
super();
[Link] = {
data1 : "ReactJS",
data3 : "Deno",
React Js
data4 : "MongoDB"
};
};
render(){
return(
<div>
<Comptwo s_o={[Link].data1}
s_t={[Link].data2}
s_three={[Link].data3}
s_f={[Link].data4}/>
</div>
};
[Link]
import "./[Link]"
return(
<div>
React Js
<h2 className="sub_one">{props.s_o}</h2>
<h2>{props.s_t}</h2>
<h2>{props.s_three}</h2>
<h2>{props.s_f}</h2>
</div>
};
[Link]
.sub_one{
color: white;
background : linear-gradient(180deg,red,black);
text-align: center;
padding: 10px;
margin: 10px;
border-radius: 20px;
[Link]
import './[Link]';
[Link](
<compone />,
[Link]('root')
);
// If you want your app to work offline and load faster, you can
change
[Link]();
**********************************************
React Js
Chapter-5(Pure-components)
Just like Pure Functions in JavaScript, a React component is
considered a Pure Component if it renders the same output for the
same state and props value. React provides the Pure Component base
class for these class components. Class components that extend the
[Link] class are treated as pure components.
3. [Link]() is called
Conclusion
Example:
> cd demo-app
Directory Structure
React Js
********************************************************
demo-app
src
[Link] (props)
[Link]
*******************************************************
[Link]
constructor(){
super();
[Link] = {
num : 1
};
};
render(){
return(
React Js
<div>
<Demo2 key1={[Link]}/>
</div>
);
};
componentDidMount(){
setInterval(()=>{
[Link]({
num : [Link]()
})
},1000);
};
};
[Link]
[Link]("child render");
return(
<div>{props.key1}</div>
React Js
};
[Link]
import './[Link]';
[Link](
<Demo1 />,
[Link]('root')
);
// If you want your app to work offline and load faster, you can
change
[Link]();
React Js
Chapter-6(Life-Cycle-Hooks)
• componentWillMount is executed before rendering, on both the
server and the client side.
Example:
> cd demo-app
Directory Structure
********************************************************
demo-app
src
[Link]
React Js
[Link]
[Link]
*******************************************************
[Link]
}
};
render(){
//it will execute immidiately after componentWillMount
//in general we will place presentation logic (JSX)
//render() is the mandatory life cycle hook
//when ever change detected in state/props automatically
this life cycle hook will execute
[Link]("parent render");
return(
<div>
{[Link]}
<br></br>
{[Link]}
<br></br><br></br>
<Child key1={[Link]}></Child>
<br></br><br></br>
<button onClick={[Link]}>Change</button>
</div>
)
};
changeSub = ()=>{
[Link]({
name : "reactjs"
});
};
/*execution flow
parent constructor
parent componentWillMount
parent render
React Js
Child constructor
Child componentWillMount
Child render
componentDidMount(){
//after render function componentDidMount will execute
//first priority goes to child component componentDidMou
nt() life cycle hooks
//in general, we will make rest api calls Ex. get reque
st
//it's recomended to change state
[Link]("parent componentDidMount");
};
/*
exuction flow
parent constructor
parent componentWillMount
parent render
Child constructor
Child componentWillMount
Child render
child componentDidMount
React Js
parent componentDidMount
*/
componentWillReceiveProps(){
//if component receives the props, then this life cycl
e hook will execute
[Link]("parent componentWillReceiveProps");
};
/*
parent render
child componentWillReceiveProps
child render
*/
shouldComponentUpdate(){
//deciding life cycle hook on state change
//true -- change the state
//false - dont change state
[Link]("parent shouldComponentUpdate");
return true;
};
//before unmounting the component react library will execute
following life cycle hooks
//these life cycle hooks used to perform the clean up operat
ions
//Ex. nullifu the instance members
//Ex. empty the state
//Ex. empty the Props ,..........
componentWillUpdate(){
[Link]("parent componentWillUpdate");
React Js
}
componentDidUpdate(){
[Link]("parent componentDidUpdate");
}
componentWillUnmount(){
[Link]("parent componentWillUnmount");
}
};
[Link]
componentWillReceiveProps(){
[Link]("child componentWillReceiveProps");
};
shouldComponentUpdate(){
[Link]("Child shouldComponentUpdate");
return false;
};
componentWillUpdate(){
[Link]("child componentWillUpdate");
}
componentDidUpdate(){
[Link]("child componentDidUpdate");
}
componentWillUnmount(){
[Link]("child componentWillUnmount");
}
};
[Link]
import './[Link]';
[Link](
<App />,
[Link]('root')
);
// If you want your app to work offline and load faster, you can
change
[Link]();
Output:
React Js
React Js
>create-react-app test-app
Directory Structure
******************************
Test-app
Src
[Link]
******************************
[Link]
expect(status).toBeInTheDocument();
});
expect(status).toBeInTheDocument();
});
>yarn test
React Js
Result:
React Js
Chapter-8(Introduction to Redux)
STATE MANAGEMENT:
elements exist.
WHAT IS REDUX:
TERMINOLOGY OF REDUX :
➢ Components in redux
Actions
Store
Reducers
Dispatch
Subscribe
State
getstate()
React Js
ACTIONS IN REDUX:
➢ Simply we call that actions are events.
➢ They are the only way we can send data from application
to Redux store. The data can be from user interactions,
API calls, or even form submissions.
➢ Actions are sent using the [Link]() method.
➢ Actions are plain JavaScript objects, and they must
have a type property to indicate the type of action to
be carried out.
➢ They must also have a payload that contains the
information that should be worked on the action.
STORE IN REDUX:
➢ The store holds the application state. There is only
one store in any Redux application.
➢ redux has one global store
➢ we can access the state stored, update the state, and
React Js
Syntax:
import { createStore } from 'redux';
REDUCERS IN REDUX:
➢ Reducers are a pure function in Redux, these Pure functions
are predictable.
➢ Reducers are the only way to change states in Redux.
➢ It is the only place where you can write logic and
calculations.
➢ reducers main job is to take “current state” and an ”
action “ and return the ”new state”
React Js
DISPATCH WORKING:
• we created store has a built-in function called dispatch
• then Call it with an action and Redux will call our
reducer with that action and then replace the state
with whatever your reducer returned.
SUBSCRIBE IN REACT:
• It helps to register a callback that Redux store will call
when an action has been dispatched.
• As soon as the Redux state has been updated, the
React Js
REDUX – INSTALLATION:
• Website: [Link]
• You can check successful installation by opening the
command prompt and type “ node –v ”
• This will show you the latest version of Node in your system.
React Js
ReduxEx
2) switch to ReduxEx
> cd ReduxEx
Directory Structure
********************************************************
ReduxEx
[Link]
*******************************************************
[Link]
const initialState = {
bal : 5000
};
switch([Link]){
case "DEPOSIT":
[Link]+=[Link];
break;
case "WITHDRAW":
[Link]-=[Link];
break;
return newState;
React Js
};
[Link](()=>{
[Link]([Link]());
});
[Link]({type:"DEPOSIT",value:5000});
[Link]({type:"DEPOSIT",value:5000});
[Link]({type:"DEPOSIT",value:5000});
[Link]({type:"WITHDRAW",value:1000});
[Link]({type:"DEPOSIT",value:5000});
[Link]({type:"WITHDRAW",value:10000});
> cd redux-ex
=> redux
=> react-redux
*****************************************
redux-ex
src
reducer
[Link]
*****************************************
*****************************************
redux-ex
src
reducer
[Link]
[Link]
React Js
[Link]
*****************************************
[Link]
const initialState = {
products : []
};
switch([Link]){
case "PRODUCTS":
return{
...state,
products:[
{"p_id":111,"p_name":"p_one","p_cost":10000},
{"p_id":222,"p_name":"p_two","p_cost":20000},
{"p_id":333,"p_name":"p_three","p_cost":30000},
{"p_id":444,"p_name":"p_four","p_cost":40000},
{"p_id":555,"p_name":"p_five","p_cost":50000}]
return state;
};
[Link]
render(){
return(
<div>
<button
onClick={[Link]}>Products</button>
<br></br>
<h6>{[Link]([Link])}</h6>
</div>
) };
};
//subscribe
return{
products : [Link]
};
//dispatch
return{
};
[Link]
import './[Link]';
//import reducer
//import Provider
[Link](
<Provider store={store}>
<App />
</Provider>,
[Link]('root')
);
// If you want your app to work offline and load faster, you can
change
[Link]();
React Js
Chapter-10(Multiple Reducers)
1) create the react application
> cd multiple-reducers-app
=> redux
=> react-redux
*****************************************
multiple-reducers-app
src
reducer
[Link]
[Link]
*****************************************
*****************************************
multiple-reducers-app
src
reducer
[Link]
[Link]
[Link]
[Link]
*****************************************
[Link]
const initialState = {
num1 : 1
};
switch([Link]){
case "UPDATE_A":
return{
...state,
}
React Js
break;
return state;
};
[Link]
const initialState = {
num2 : 1
};
switch([Link]){
case "UPDATE_B":
return{
...state,
num2:state.num2 + [Link]
break;
return state;
};
[Link]
render(){
return(
<div>
Num1 : <span
style={{color:"red",marginRight:100}}>{[Link].num1}</span>
Num2 : <span
style={{color:"red"}}>{[Link].num2}</span>
<br></br><br></br><br></br>
</div>
};
return{
React Js
num1 : [Link].num1,
num2 : [Link].num2
};
return{
updateNum1 : (data)=>{
dispatch({type:"UPDATE_A",value:data}) },
updateNum2 : (data)=>{
dispatch({type:"UPDATE_B",value:data}) }
};
[Link]
import './[Link]';
rA : reducerA,
rB : reducerB
});
[Link](
<Provider store={store}>
<App />
</Provider>,
[Link]('root')
);
// If you want your app to work offline and load faster, you can
change
[Link]();
React Js
Chapter-11(Saga Middleware)
> cd redux-saga-example
=> redux
=> react-redux
**************************************************
redux-saga-example
src
saga
[Link]
*********************************************************
React Js
[Link]
function* ageUpAsync() {
yield delay(4000);
yield put({ type: "AGE_UP_ASYNC", value: 1 });
}
function* ageDownAsync(){
yield delay(2000);
yield put({type:"AGE_DOWN_ASYNC",value:1});
};
**************************************************
redux-saga-example
src
store
[Link]
*********************************************************
React Js
[Link]
[Link]
const initialState = {
age: 20
};
switch ([Link]) {
case "AGE_UP_ASYNC":
[Link] += [Link];
break;
case "AGE_DOWN_ASYNC":
[Link] -= [Link];
break;
}
return newState;
};
**************************************************
redux-saga-example
src
React Js
[Link]
[Link]
*********************************************************
[Link]
import "./[Link]";
[Link](watchAgeUp);
[Link](
<Provider store={store}>
<App />
</Provider>,
React Js
[Link]("root")
);
[Link]
return {
onAgeUp: () => dispatch({ type: "AGE_UP", value: 1 }),
onAgeDown: () => dispatch({ type: "AGE_DOWN", value: 1 })
};
};
export default connect(
mapStateToProps,
mapDispachToProps
)(App);
OutPut:
React Js
Chapter-12(thunk Middleware)
1) create the react application
> cd redux-thunk-example
Directory Structure
**************************************************
redux-thunk-example
src
actions
[Link]
reducer
[Link]
[Link]
[Link]
*********************************************************
React Js
**************************************************
redux-thunk-example
src
reducer
[Link]
*********************************************************
[Link]
const initialState = {
bal : 5000
};
switch([Link]){
case "WITHDRAW":
[Link]-=[Link];
break;
case "DEPOSIT":
[Link]+=[Link];
break;
React Js
return newState;
};
**************************************************
redux-thunk-example
src
actions
[Link]
*********************************************************
[Link]
return {type:"WITHDRAW",value:val};
};
return {type:"DEPOSIT",value:val};
};
return (dispatch)=>{
setTimeout(()=>{
dispatch(onDepositAsync(val));
},5000);
};
redux-thunk-example
src
[Link]
*********************************************************
[Link]
import './[Link]';
[Link](
<Provider store={store}>
<App />
</Provider>,
[Link]('root')
);
// If you want your app to work offline and load faster, you can
change
[Link]();
**************************************************
redux-thunk-example
src
[Link]
*********************************************************
[Link]
import "./[Link]";
render(){
return(
<div className="App">
<h1>Balanace:<span style={{color:"green"}}>{[Link]
[Link]}</span></h1>
<button onClick={[Link]}><b>Withdraw</b
></button>
<button onClick={[Link]}><b>Deposit</b><
/button>
</div>
)
React Js
return{
bal : [Link]
};
return{
};
OutPut:
React Js
React Js
For example :
>cd spa-app
Directory Struture
******************************************************
Spa-app
Src
[Link]
[Link]
[Link]
[Link]
[Link]
React Js
******************************************************
[Link]
[Link]
[Link]
React Js
[Link]
//import BrowserRouter
//import NavLink
//import Route
render(){
return(
<Router>
<div>
<NavLink to="/"
exact strict
activeStyle={{color:"green"}}
style={{marginRight:100}}><b>About</b></NavLink>
<NavLink to="/home"
exact strict
activeStyle={{color:"green"}}
style={{marginRight:100}}><b>Home</b></NavLink>
<NavLink to="/contact"
exact strict
activeStyle={{color:"green"}}
style={{marginRight:100}}><b>Contact</b></NavLink>
<br></br><br></br>
</div>
</Router>
};
[Link]
import './[Link]';
[Link](
<[Link]>
<App />
</[Link]>,
[Link]('root')
);
React Js
// If you want your app to work offline and load faster, you can
change
[Link]();
React Js
Step 1.
******************************************
user : system
password : root
connectString : localhost/XE
table : auth
******************************************
step 2.
➢ Create-react-app parameters-app
Step 3.
➢ cd parameters-app
Step 4.
1) express
2) oracledb
3) body-parser
4) cors
Step 5.
**********************************************
parameters-app
server
[Link]
React Js
**********************************************
[Link]
[Link](cors());
[Link]([Link]());
[Link]([Link]({extended:false}));
[Link]("/login",(req,res)=>{
[Link]({user:"system",
password:"root",
connectString:"localhost/XE"},(err,c
onnection)=>{
else{
else{
if([Link]>0){
[Link]({"login":"success"});
}else{
[Link]({"login":"fail"});
});
});
});
React Js
[Link](8080);
Step 6.
➢ cd parameters-app/server
➢ node server
React Js
Step 7.
[Link] (POST)
React Js
Step 8.
Step 9.
**************************************
Parameters-app
Src
[Link]
[Link]
[Link]
*************************************
[Link]
constructor(){
super();
[Link] = {
};
render(){
return(
<div>
<fieldset>
<legend>Login</legend>
<br></br><br></br>
<br></br><br></br>
<button onClick={[Link]}>Login</button>
</fieldset>
</div>
};
login = ()=>{
React Js
[Link]("[Link]
[Link],
"upwd":[Link]
[Link]})
.then((posRes)=>{
[Link](`/dashboard/${this.s
[Link]}`);
}else{
alert("Login Fail");
},(errRes)=>{
[Link](errRes);
});
};
};
[Link]
render(){
return(
React Js
<div>
<h1 style={{color:"red"}}>{[Link]
ms.param1}</h1>
</div>
};
};
[Link]
render(){
return(
<div>
<Router>
</Router>
React Js
</div>
};
Step 10.
Terminal-1
➢ cd parameters-app/server
➢ node server
Terminal-2
➢ cd parameters-app
➢ yarn start
Output:
React Js
React Js
Lazy loading:
>cd Lazy-app
Directory Struture
******************************************************
Lazy-app
Src
Components
[Link]
[Link]
[Link]
******************************************************
[Link]
};
[Link]
import "./[Link]";
render() {
return (
<div className="App">
<header className="App-header">
<div>another component</div>
<Suspense fallback={<div>Loading.....</div>}>
<MyComp />
</Suspense>
</header>
</div>
React Js
);
[Link]
import './[Link]';
[Link](
<[Link]>
<App />
</[Link]>,
[Link]('root')
);
// If you want your app to work offline and load faster, you can
change
React Js
[Link]();
Output:
React Js
Chapter-14(Crud Operations)
CRUD Operations on MySQL DataBase
---------------------------------
SQL Queries
----------
Default Password : root
*******************************
host : localhost
user : root
Password: root
database: reactjs
table : employee
********************************
> cd crud-app
=> express
React Js
=> cors
=> mysql
=> body-parser
**************************
crud-app
server
[Link]
*************************
> cd crud-app/server
Terminal-2
----------
> cd crud-app
*************************************
crud-app
server
[Link]
src
[Link]
React Js
[Link]
[Link]
[Link]
*************************************
[Link]
**********
//import modules
//require() function used to import the modules
let express = require("express");
let mysql = require("mysql");
let cors = require("cors");
let bodyparser = require("body-parser");
[Link]
*********
<div>
<table border="1"
cellPadding="10px"
cellSpacing="10px"
align="center">
<thead
style={{backgroundColor:"grey"}}>
<tr>
<th>SNO</th>
<th>ENO</th>
<th>ENAME</th>
<th>ESAL</th>
</tr>
</thead>
<tbody>
{[Link]((element,index)=>(
<tr>
<td>{index+1}</td>
<td>{[Link]}</td>
<td>{[Link]}</td>
<td>{[Link]}</td>
</tr>
))}
</tbody>
</table>
<hr style={{height:5,backgroundColor:"grey"}}></hr>
<br></br><br></br>
React Js
<Insert />
<hr style={{height:5,backgroundColor:"grey"}}></hr>
<br></br><br></br>
<Update />
<hr style={{height:5,backgroundColor:"grey"}}></hr>
<br></br><br></br>
<Delete />
</div>
)
};
};
[Link]
**********
constructor(){
super();
[Link]={
React Js
status:""
};
};
render(){
return(
<div>
<fieldset>
<legend>Insert</legend>
<input type="number"
ref="eno"/>
<br></br><br></br>
<input type="text"
ref="ename"/>
<br></br><br></br>
<input type="number"
ref="esal"/>
React Js
<br></br><br></br>
<button onClick={[Link]}>Insert</button>
<br></br>
<h1>{[Link]}</h1>
</fieldset>
</div>
};
insert = ()=>{
let record = {
"eid":[Link],
"ename":[Link],
"esal":[Link]
};
[Link]("[Link]
record)
.then((posRes)=>{
[Link]({
status:[Link]
React Js
})
},(errRes)=>{
[Link](errRes);
});
};
};
[Link]
*********
constructor(){
super();
[Link]={
status : ""
};
};
render(){
return(
<div>
React Js
<fieldset>
<legend>Update</legend>
<input type="number"
ref="eid"
placeholder="employee id"/>
<br></br><br></br>
<input type="text"
ref="ename"
placeholder="employee name"/>
<br></br><br></br>
<input type="number"
ref="esal"
placeholder="Employee Sal"/>
<br></br><br></br>
<button onClick={[Link]}>Update</button>
<h1>{[Link]}</h1>
</fieldset>
</div>
};
update = ()=>{
React Js
let record = {
"eid":[Link],
"ename":[Link],
"esal":[Link]
};
[Link]("[Link]
.then((posRes)=>{
[Link]({
status :[Link]
})
},(errRes)=>{
[Link](errRes);
});
};
};
[Link]
**********
super();
[Link]={
status : ""
};
};
render(){
return(
<div>
<fieldset>
<legend>Delete</legend>
<input type="number"
ref="eid"
placeholder="Employee ID"/>
<br></br><br></br>
<button onClick={[Link]}>Delete</button>
<h1>{[Link]}</h1>
</fieldset>
</div>
)
};
remove = ()=>{
let record = {
"eid":[Link]
};
[Link]("[Link]
.then((posRes)=>{
[Link]({
status:[Link]
})
React Js
},(errRes)=>{
[Link](errRes);
});
};
};
React Js
Chapter-15(Mini Project)
React Js
MySQL
MongoDB
SQLServer
module in miniproject.
authentication system.
Multiple Modules
- Index
- Login
- Dashboard
@About
@Home
@Portfolio
Implementation
--------------
[Link]
upwd varchar(20));
*********************************
host : localhost
user : root
password: root
database: reactjs
tables : login_details
about
*********************************
> mongod
> mongo
> [Link]("home");
> [Link]({sno:1,name:"reactjs"});
> [Link]();
******************************
protocol : mongodb
port : 27017
host : localhost
database : reactjs
collection : home
******************************
******************************
User : sa
Password : 123
server : localhost
database : reactjs
table : portfolio
******************************
React Js
> cd miniproject-app
=> express
=> mysql
=> mssql
=> body-parser
=> cors
=> jwt-simple
*********************************************
miniproject-app =>server
config
mysql_properties.js
mysql_connection.js
mssql_properties.js
[Link]
[Link]
[Link]
login
[Link]
about
[Link]
home
[Link]
portfolio
[Link]
logout
[Link]
[Link]
***********************************************
React Js
mysql_properties.js
let obj = {
host : "localhost",
React Js
user : "root",
password: "root",
database: "reactjs"
};
[Link] = obj;
mysql_connection.js
//import db details
let conn = {
getConnection : ()=>{
return [Link](obj);
};
//export conn
[Link] = conn;
mssql_properties.js
let obj = {
React Js
server : "localhost",
user : "sa",
password: "123",
database: "reactjs"
};
[Link] = obj;
[Link]
let obj = {
token : ""
};
[Link] = obj;
[Link]
return [Link](data,password);
};
React Js
[Link] = my_fun;
[Link]
[Link](c_token);
[Link](s_token);
next();
}else{
[Link]("UnAuthorized User....!");
};
[Link] = auth;
React Js
[Link]
[Link] =
require("express").Router().post("/",(req,res)=>{
(err,records,fields)=>{
if([Link]>0){
uname:[Link],
upwd:[Link]
},"hr@[Link]");
[Link] = token;
[Link]({login:"success",token:token});
}else{
[Link]({login:"fail"});
});
});
[Link]
[Link] =
require("express").Router().get("/",[auth],(req,res)=>{
[Link](`select * from
about`,(err,records,fields)=>{
[Link](records);
});
});
[Link]
React Js
[Link] =
require("express").Router().get("/",[auth],(req,res)=>{
[Link]("mongodb://localhost:27017/reactjs",(err,db)=
>{
[Link]("home").find().toArray((err,array)=>{
[Link](array);
});
});
});
[Link]
[Link] =
require("express").Router().get("/",[auth],(req,res)=>{
React Js
[Link](obj,(err)=>{
if(err){
[Link]("Error...!");
}else{
[Link](`select * from
portfolio`,(err,records)=>{
if(err){
[Link]("Error...!");
}else{
[Link](records);
[Link]();
});
});
});
[Link]
[Link] =
require("express").Router().get("/",[auth],(req,res)=>{
[Link] = "";
[Link]({logout:"success"});
});
[Link]
[Link]([Link]());
[Link]([Link]({extended:false}));
[Link](cors());
[Link]("/login",require("./login/login"));
[Link]("/about",require("./about/about"));
[Link]("/home",require("./home/home"));
[Link]("/portfolio",require("./portfolio/portfolio"));
[Link]("/logout",require("./logout/logout"));
[Link](8080);
Step 9:
> cd miniproject-app
> cd server
Step 10.
Step 11.
> cd miniproject-app
Step 12.
*************************************
Miniproject-app
src
[Link]
[Link]
[Link]
[Link]
***************************************
[Link]
import "./[Link]";
render(){
return(
<div className="App">
<fieldset>
<legend>Login</legend>
React Js
<label
style={{marginRight:40}}>Uname:</label>
<input type="text"
ref="uname"
placeholder="User Name"></input>
<br></br><br></br>
<label
style={{marginRight:40}}>Upwd:</label>
<input type="password"
ref="upwd"
placeholder="Password"></input>
<br></br><br></br>
<button onClick={[Link]}>Login</button>
</fieldset>
</div>
login = ()=>{
[Link]("[Link]
'uname':[Link],
'upwd':[Link]
React Js
}).then((posRes)=>{
[Link]("login_details",str);
//navigate to Dashboard
[Link]('/dashboard')
},(errRes)=>{
[Link](errRes);
});
};
};
[Link]
import "./[Link]";
constructor(){
super();
[Link] = {
};
React Js
render(){
return(
<div className="App">
<h1 style={{color:"red"}}>{[Link]}</h1>
</div>
};
componentDidMount(){
[Link]([Link]);
[Link]("[Link]
n}})
.then((posRes)=>{
[Link](posRes);
[Link]({
res:str
})
},(errRes)=>{
React Js
[Link](errRes);
});
};
[Link]
import "./[Link]";
constructor(){
super();
[Link] = {
};
render(){
return(
<div className="App">
<h1
style={{color:"green"}}>{[Link]}</h1>
</div>
)
React Js
};
componentDidMount(){
let token =
[Link]([Link]("login_details"))
.token;
[Link]("[Link]
then((posRes)=>{
[Link]({
res:[Link]([Link])
});
},(errRes)=>{
[Link](errRes);
});
};
};
[Link]
import "./[Link]";
constructor(){
React Js
super();
[Link] = {
};
render(){
return(
<div className="App">
<h1 style={{color:"blue"}}>{[Link]}</h1>
</div>
};
componentDidMount(){
let token =
[Link]([Link]("login_details"))
.token;
[Link]("[Link]
{headers:{token:token}}).then((posRes)=>{
[Link]({
res:[Link]([Link])
});
React Js
},(errRes)=>{
[Link](errRes);
});
};
};
Step 13.
> cd miniproject-app
Step 14.
*************************************
Miniproject-app
Src
[Link]
**************************************
[Link]
//import Route
render(){
return(
<Router>
<div>
<NavLink to="/about"
activeStyle={{color:"green"}}
exact strict
style={{marginRight:100}}>About</NavLink>
<NavLink to="/home"
activeStyle={{color:"green"}}
React Js
exact strict
style={{marginRight:100}}>Home</NavLink>
<NavLink to="/portfolio"
activeStyle={{color:"green"}}
exact strict
style={{marginRight:100}}>Portfolio</NavLink>
<button
onClick={[Link]}>Logout</button>
<br></br><br></br>
</div>
</Router>
) };
logout = ()=>{
[Link]("[Link]
}})
.then((posRes)=>{
[Link]("login_details");
[Link]('/')
},(errRes)=>{
[Link](errRes);
});
};};
Step 15.
*************************************
Miniproject-app
Src
[Link]
React Js
**************************************
[Link]
render(){
return(
<Router>
<div>
</div>
</Router>
};
Step 16.
React Js
*************************************
Miniproject-app
Src
[Link]
**************************************
[Link]
import './[Link]';
// If you want your app to work offline and load faster, you can
change
[Link]();
React Js
Step 17.
//Terminal-1
> cd miniproject-app
> cd server
//Terminal-2
> mongod
//Terminal-3
Chapter-16(Crud Operations-Thunk)
CRUD Operations on MySQL DataBase
---------------------------------
React Js
Ex.
MySQL
1) express
2) mysql
3) body-parser
4) cors
React Js
> cd MySQL
****************************************
host : localhost
user : root
password : root
database : fullstack
table : products
****************************************
*********************************
MySQL
config
React Js
db_properties.js
db_connection.js
fetch
[Link]
insert
[Link]
update
[Link]
delete
[Link]
[Link]
************************************
db_properties.js
let conn = {
getConnection : ()=>{
return [Link](obj);
};
[Link] = conn;
db_connection.js
let obj = {
host : "localhost",
user : "root",
password : "root",
database : "fullstack_9pm"
};
[Link] = obj;
[Link]
[Link](`select * from
products`,(err,records,fields)=>{
else{
[Link](records);
});
});
[Link] = fetch;
[Link]
'${[Link].p_name}',
${[Link].p_cost})`,(err,result)=>{
else{
[Link]({"insert":"success",record:{"p_id":`${[Link].p_id}`,
React Js
"p_name":`${[Link].p_name}`,
"p_cost":`${[Link].p_cost}`}
});
});
});
[Link] = insert;
[Link]
else{
[Link]({update:"success",record:{"p_id":[Link].p_id,
"p_name":[Link].p_name,
React Js
"p_cost":[Link].p_cost}});
});
});
[Link] = update;
[Link]
else{
[Link]({delete:"success","p_id":[Link].p_id});
});
});
[Link] = remove;
[Link]
[Link](cors());
[Link]([Link]());
[Link]([Link]({extended:false}));
[Link]("/fetch",require("./fetch/fetch"));
[Link]("/insert",require("./insert/insert"));
[Link]("/update",require("./update/update"));
[Link]("/delete",require("./delete/delete"));
[Link](8080);
> cd MySQL
8) switch to crud-app
> cd crud-app
Terminology
Actions
-------
Ex.
Reducer
-------
=> redux-thunk
=> redux
=> react-redux
=> bootstrap
=> react-bootstrap
=> axios
React Js
****************************
public
[Link]
************************
[Link]
<script
src="[Link]
crossorigin></script>
<script
src="[Link]
[Link]"
crossorigin></script>
React Js
<script
src="[Link]
[Link]"
crossorigin></script>
<link rel="stylesheet"
href="[Link]
[Link]"
integrity="sha384-
Vkoo8x4CGsO3+Hhxv8T/Q5PaXtkKtu6ug5TOeNV6gBiFeWPGFN9MuhOf23Q9Ifjh
"
crossorigin="anonymous"
/>
<script
src="[Link]
**********************************
src
actions
[Link]
React Js
*********************************
[Link]
/***************************************************/
return (dispatch)=>{
return [Link]("[Link]
.then((posRes)=>{
dispatch( fun_one([Link]) );
},(errRes)=>{
[Link](errRes);
});
};
return {type:"FETCH",value:records};
};
/****************************************************/
/*****************************************************/
return (dispatch)=>{
return
[Link]("[Link]
.then((posRes)=>{
dispatch(fun_two([Link]));
},(errRes)=>{
[Link](errRes);
});
};
};
return {type:"DELETE",value:result};
};
/**************************************************/
/************************************************ */
return (dispatch)=>{
return [Link]("[Link]
.then((posRes)=>{
dispatch(fun_three([Link]));
},(errRes)=>{
React Js
[Link](errRes);
});
};
};
/******************************************** */
/*********************************************** */
return (dispatch)=>{
return [Link]("[Link]
.then((posRes)=>{
dispatch(fun_four([Link]));
},(errRes)=>{
[Link](errRes);
});
};
};
/********************************************** */
****************************************
src
reducer
[Link]
****************************************
[Link]
const initialState = {
data : []
};
switch([Link]){
case "FETCH":
[Link] = [];
return{
...state,
data : [Link]([Link])
}
React Js
break;
case "INSERT":
case "UPDATE":
case "DELETE":
return{
...state,
status : [Link]
break;
return state;
};
****************************************
src
[Link]
****************************************
React Js
[Link]
import './[Link]';
[Link](
<Provider store={store}>
<App />
</Provider>,
[Link]('root')
);
// If you want your app to work offline and load faster, you can
change
React Js
[Link]();
***********************************
src
[Link]
**********************************
[Link]
constructor(){
super();
[Link] = {
React Js
status : false,
insertPopup : false,
updatePopup : false
};
};
componentDidMount(){
[Link]();
};
showPopup = (msg)=>{
[Link]({
updatePopup:false,
insertPopup : true,
status : true
})
}else{
[Link]({
insertPopup : false,
updatePopup : true,
status : true
})
}
React Js
};
closePopup = ()=>{
[Link]({
status : false
})
};
deleteRecord = (id)=>{
[Link](id);
};
save = ()=>{
if([Link]){
"p_name" : [Link].p_name.value,
"p_cost" : [Link].p_cost.value};
[Link](obj);
else if([Link]){
"p_name" : [Link].p_name.value,
React Js
"p_cost" : [Link].p_cost.value};
[Link](obj);
[Link]();
[Link]();
};
render(){
return(
<Button className="float-right"
onClick={()=>{ [Link]("insert")
}}>Add <i className="fas fa-plus"></i>
</Button>
<Modal show={[Link]}
onHide={[Link]}
size="sm"
centered>
React Js
<[Link] closeButton>
<[Link]>Add/Update</[Link]>
</[Link]>
<[Link]>
<Form>
<[Link]>
<[Link]>P_ID</[Link]>
<[Link] type="number"
placeholder="Enter P_ID"
ref="p_id"></[Link]>
</[Link]>
<[Link]>
<[Link]>P_NAME</[Link]>
<[Link] type="text"
placeholder="Enter P_NAME"
ref="p_name"></[Link]>
</[Link]>
<[Link]>
React Js
<[Link]>P_COST</[Link]>
<[Link] type="number"
placeholder="Enter P_COST"
ref="p_cost"></[Link]>
</[Link]>
</Form>
</[Link]>
<[Link]>
<Button varient="danger"
onClick={[Link]}>Close</Button>
</[Link]>
</Modal>
<Table bordered
variant="dark"
size="sm"
React Js
hover
striped
className="text-center">
<thead>
<tr>
<th>SNO</th>
<th>ID</th>
<th>NAME</th>
<th>COST</th>
<th>EDIT</th>
<th>DELETE</th>
</tr>
</thead>
<tbody>
{[Link]((element,index)=>(
<tr key={index}>
<td>{index+1}</td>
<td>{element.p_id}</td>
<td>{element.p_name}</td>
<td>{element.p_cost}</td>
React Js
</tr>
))}
</tbody>
</Table>
</Container>
};
if([Link]("status")){
let id = [Link]((element,index)=>{
});
React Js
[Link](id,1);
[Link]([Link]);
[Link]((element,index)=>{
if(element.p_id == [Link].p_id){
element.p_name = [Link].p_name;
element.p_cost = [Link].p_cost;
});
return{
data : [Link],
status : [Link]
}
React Js
};
return{
deleteRecord : (id)=>{
dispatch([Link]({"p_id":id})) },
addProduct : (record)=>{
dispatch([Link](record)) },
updateProduct : (record)=>{
dispatch([Link](record)) }
};
Output:
React Js
Chapter-17(MiniProject-Saga)
Step 1) install MongoDB
website :
[Link]
win32-x86_64-[Link]/download
file : mongodb-win32-x86_64-[Link]
c:/data/db
********************************************
MongoDB Queries
********************************************
> mongod
> mongo
> [Link]("login_details");
> [Link]("angular");
> [Link]("react");
> [Link]("deno");
> db.login_details.insert({"uname":"admin","upwd":"admin"});
> db.login_details.find().pretty();
> [Link]().pretty();
> [Link]().pretty();
> [Link]().pretty();
****************************************************
host : localhost
protocol : mongodb
port : 27017
database : miniproject@4pm
collections : login_details
angular
react
deno
****************************************************
=> express
=> cors
React Js
=> body-parser
=> jwt-simple
**********************************
miniproject@4pm
config
[Link]
[Link]
[Link]
React Js
login
[Link]
angular
[Link]
react
[Link]
deno
[Link]
logout
[Link]
[Link]
**********************************
[Link]
React Js
return [Link](data,password);
};
[Link] = getnerateToken;
[Link]
let obj = {
"token":""
};
[Link] = obj;
[Link]
//this file used to compare the client side token with the
server side token (middleware)
if(c_token == [Link]){
next();
}else{
};
[Link] = auth;
[Link]
[Link]("mongodb://localhost:27017/authproject",(err,db
)=>{
else{
[Link]("login_details")
.find({"uname":[Link],"upwd":[Link]})
.toArray((err,array)=>{
else{
if([Link]>0){
let token =
generateToken({"uname":[Link],
"upwd":[Link]},"hr@[Link]");
[Link] = token;
React Js
[Link]({login:"success",token:token})
}else{
[Link]({login:"fail"});
});
});
});
[Link] = login;
[Link]
let angular =
require("express").Router().get("/",[require("../config/auth")],
(req,res)=>{
[Link]("mongodb://localhost:27017/authproject",(err,db
)=>{
else{
[Link]("angular").find().toArray((err,array)=>{
React Js
else{
[Link](array);
});
});
});
[Link] = angular;
[Link]
let react =
require("express").Router().get("/",[require("../config/auth")],
(req,res)=>{
[Link]("mongodb://localhost:27017/authproject",(err,db
)=>{
else{
[Link]("react").find().toArray((err,array)=>{
else{
React Js
[Link](array);
});
});
});
[Link] = react;
[Link]
let deno =
require("express").Router().get("/",[require("../config/auth")],
(req,res)=>{
[Link]("mongodb://localhost:27017/authproject",(err,db
)=>{
else{
[Link]("deno").find().toArray((err,array)=>{
else{
[Link](array);
}
React Js
});
});
});
[Link] = deno;
[Link]
let logout =
require("express").Router().get("/",[require("../config/auth")],
(req,res)=>{
[Link] = "";
[Link]({"logout":"success"});
});
[Link] = logout;
[Link]
[Link](cors());
[Link]([Link]());
[Link]([Link]({extended:false}));
React Js
[Link]("/login",require("./login/login"));
[Link]("/angular",require("./angular/angular"));
[Link]("/react",require("./react/react"));
[Link]("/deno",require("./deno/deno"));
[Link]("/logout",require("./logout/logout"));
[Link](8085);
> cd auth-proj
=> axios
=> redux
=> react-redux
=> redux-saga
=> react-router-dom
=> @material-ui/core
=> @material-ui/icons
<!DOCTYPE html>
<html lang="en">
<head>
<meta
name="description"
/>
<!--
-->
React Js
<!--
-->
<link rel="stylesheet"
href="[Link]
,700&display=swap" />
<link rel="stylesheet"
href="[Link]
/>
<title>React App</title>
</head>
<body>
React Js
<div id="root"></div>
<!--
The build step will place the bundled scripts into the
<body> tag.
-->
</body>
</html>
********************************
auth-proj
src
action
[Link]
********************************
[Link]
function login_call(data){
return [Link]("[Link]
};
function* login(action) {
if([Link] == "success"){
React Js
[Link]("login_details",[Link](res.d
ata));
//yield call(forwardTo,"/dashboard");
//[Link]("/dashboard");
[Link] = `/dashboard`
}else{
//navigate to Error
[Link]("error");
};
function angular_call(){
};
React Js
function *angular(action){
yield put({type:"ANGULAR",value:[Link]});
function react_call(){
return
[Link]("[Link] :
[Link]}});
};
function *react(action){
yield put({type:"REACT",value:[Link]});
};
function deno_call(){
React Js
return
[Link]("[Link] :
[Link]}});
};
function *deno(action){
};
function logout_call(){
return
[Link]("[Link] :
[Link]}});
};
React Js
function *logout(action){
if([Link] == "success"){
[Link]("login_details");
yield put({type:"LOGOUT",value:res});
[Link] = `/`
}else{
[Link]("error");
};
yield takeLatest("LOGOUT_ACTION",logout );
};
function forwardTo(location) {
[Link](location);
React Js
****************************************
auth-proj
reducer
[Link]
***************************************
[Link]
switch([Link]){
case "LOGIN":
return{
...state,
login : [Link]
break;
case "ANGULAR":
case "REACT":
case "DENO":
return{
...state,
React Js
msg : [Link]
break;
case "LOGOUT":
return{
...state
break;
};
return state;
};
[Link]
import "./[Link]";
[Link](main_fun);
[Link](
<Provider store={store}>
<App />
</Provider>,
[Link]("root")
);
****************************************
auth-proj
src
[Link]
[Link]
[Link]
[Link]
React Js
[Link]
App.s
***************************************
[Link]
render(){
return(
<div>
<fieldset>
<legend>Login</legend>
<input type="text"
placeholder="user name"
ref="uname"/>
<br></br><br></br>
<input type="password"
placeholder="user password"
ref="upwd"/>
<br></br><br></br>
React Js
<button
onClick={()=>{[Link]({"uname":[Link],
"upwd":[Link]})}}>Login</button>
</fieldset>
</div>
};
};
//subscribe
[Link](state);
};
//dispatch
return{
login : (obj)=>{
dispatch({type:"LOGIN_ACTION",value:obj}) }
};
React Js
[Link]
render(){
return(
<Router>
<NavLink to="/angular"
activeStyle={{color:"red"}}
exact strict
style={{marginRight:100}}>Angular</NavLink>
<NavLink to="/reactjs"
React Js
activeStyle={{color:"red"}}
exact strict
style={{marginRight:100}}>React</NavLink>
<NavLink to="/deno"
activeStyle={{color:"red"}}
exact strict
style={{marginRight:100}}>Deno</NavLink>
<button
onClick={[Link]}>Logout</button>
<br></br><br></br>
</Router>
};
React Js
return{
};
return{
logout : ()=>{
[Link]("hello");
dispatch({type:"LOGOUT_ACTION",value:{}}) }
};
[Link]
componentDidMount(){
[Link]();
};
render(){
return(
<div>
{[Link]([Link])}
</div>
};
[Link](state);
return{
angular : state
};
return{
getAngularData : ()=>{
dispatch({type:"ANGULAR_ACTION",value:{}}) }
}
React Js
[Link]
componentDidMount(){
[Link]();
};
render(){
return(
<div>
{[Link]([Link])}
</div>
};
return{
msg : state
}
React Js
};
return{
getReactModuleData : ()=>{
dispatch({type:"REACT_ACTION",value:{} }) }
};
[Link]
componentDidMount(){
[Link]();
};
render(){
return(
<div>
{[Link]([Link])}
</div>
}
React Js
};
return{
msg : state
};
return{
getDenoData : ()=>{
dispatch({type:"DENO_ACTION",value:{}}) }
};
[Link]
render(){
return(
React Js
<Router>
</Router>
}};
Step 14)
//Terminal-1
> cd miniproject-app
> cd server
//Terminal-2
> mongod
//Terminal-3
>cd auth-proj
Chapter-18(Validations Example)
In this example we will store the data into database (SQLServer)
*************************************************
user : sa
password : 123
server : localhost
database : reactjs
table : registration
************************************************
> cd registration-app
=> express
=> cors
=> body-parser
=> axios
****************************************
registration-app
server
[Link]
****************************************
[Link]
//ports communication
[Link](cors());
[Link]([Link]());
[Link]([Link]({extended:false}));
[Link]("/registration",(req,res)=>{
//connect to SQLServer
[Link]({
user:"sa",
password:"123",
server:"localhost",
React Js
database:"reactjs"
},(err)=>{
else{
else{
[Link]({registration:"success"});
})
});
});
[Link](8080);
> cd registration-app
> cd server
Terminal-2
----------
> cd registration-app
[Link]
import "../node_modules/bootstrap/dist/css/[Link]";
constructor(){
super();
[Link] = {
"country":"",
"countryValid":false,
React Js
"Female":false,
"Male":false,
"genderValid":false,
"English":false,
"Hindi":false,
"Telugu":false,
"fname":"",
"lname":"",
"email":"",
"dob":"",
"dobValid":false,
"emailValid":false,
"fnameValid":false,
"lnameValid":false,
"formValid":false,
"languagesValid":false,
"formErrors":{"fname":"",
"lname":"",
"email":"",
"dob":"",
"languages":"",
"gender":"",
React Js
"country":""}
};
};
register = (event)=>{
let gender="";
if([Link][key]===true){
[Link](key);
};
gender = key
};
let record = {
"fname":[Link],
"lname":[Link],
React Js
"email":[Link],
"dob":[Link],
"gender":gender,
"languages":languages,
"country":[Link]
};
[Link]("[Link]
.then((posRes)=>{
[Link](posRes);
},(errRes)=>{
[Link](errRes);
});
};
handleChange = (event)=>{
switch(name){
case "English":
React Js
[Link](prevState=>({
English : ![Link]
}),()=>{[Link](name,value)});
break;
case "Telugu":
[Link](prevState=>({
Telugu : ![Link]
}),()=>{[Link](name,value)});
break;
case "Hindi":
[Link](prevState=>({
Hindi : ![Link]
}),()=>{[Link](name,value)});
break;
case "Gender":
[Link](prevState=>({
Male : ![Link]
}),()=>{[Link](name,value)});
}else{
[Link](prevState=>({
Female : ![Link]
React Js
}),()=>{[Link](name,value)});
break;
default:
[Link]({
[name]:value
},()=>{[Link](name,value)});
break;
};
validateForm(name,value){
switch(name){
case "country":
React Js
l_countryValid = [Link]<0;
break;
case "Gender":
break;
case "fname":
l_fnameValid = [Link]>6;
break;
case "lname":
l_lnameValid = [Link]>3;
break;
case "email":
l_emailValid = [Link](/^([\w.%+-]+)@([\w-
]+\.)+([\w]{2,})$/i);;
React Js
break;
case "dob":
l_dobValid= [Link]>8;
break;
case "English":
case "Telugu":
case "Hindi":
l_formErrors.languages = l_languagesValid?"":"s
elect at least one language"
break;
};
[Link]({
fnameValid:l_fnameValid,
lnameValid:l_lnameValid,
emailValid:l_emailValid,
dobValid:l_dobValid,
React Js
languagesValid:l_languagesValid,
genderValid : l_genderValid,
formErrors:l_formErrors
},[Link] );
};
finalFun(){
[Link]({
formValid : [Link],
formValid : [Link],
formValid : [Link],
formValid : [Link],
formValid : [Link],
formValid : [Link]
})
};
render(){
return(
<div className="col-md-4">
<label>First Name</label>
</div>
<div className="col-md-8">
<input type="text"
name="fname"
value={[Link]}
required
onChange={[Link]}></in
put>
<h5 style={{color:"red"}}>{[Link]
[Link]}</h5>
</div>
</div>
<div className="row">
React Js
<div className="col-md-4">
<label>Last Name</label>
</div>
<div className="col-md-8">
<input type="text"
name="lname"
value={[Link]}
required
onChange={[Link]}></in
put>
<h5 style={{color:"red"}}>{[Link]
[Link]}</h5>
</div>
</div>
<div className="row">
<div className="col-md-4">
<label>Email</label>
</div>
React Js
<div className="col-md-8">
<input type="email"
name="email"
value={[Link]}
placeholder="Enter Email"
required
onChange={[Link]}></in
put>
<h5 style={{color:"red"}}>{[Link]
[Link]}</h5>
</div>
</div>
<div className="row">
<div className="col-md-4">
<label>Date Of Birth</label>
</div>
<div className="col-md-8">
<input type="date"
name="dob"
value={[Link]}
React Js
required
onChange={[Link]}></in
put>
<h5 style={{color:"red"}}>{[Link]
[Link]}</h5>
</div>
</div>
<div className="row">
<div className="col-md-4">
<label>Languages</label>
</div>
<div className="col-md-8">
<input type="checkbox"
checked={[Link]}
name="English"
value="English"
onChange={[Link]}/> <b
>English</b>
<input type="checkbox"
className="ml-1"
React Js
name="Telugu"
value="Telugu"
checked={[Link]}
onChange={[Link]}/> <b
>Telugu</b>
<input type="checkbox"
className="ml-1"
name="Hindi"
value="Hindi"
checked={[Link]}
onChange={[Link]}/> <b
>Hindi</b>
</div>
<h5 style={{color:"red"}}>{[Link]
[Link]}</h5>
</div>
<br></br>
<div className="row">
<div className="col-md-4">
<label>Gender</label>
</div>
<div className="col-md-8">
React Js
<input type="radio"
name="Gender"
value="Male"
checked={[Link]}
onChange={[Link]}/> <b
>Male</b>
<input type="radio"
name="Gender"
value="Female"
checked={[Link]}
onChange={[Link]}
className="ml-2"/> <b>Female</b>
</div>
</div>
<br></br><br></br>
<div className="row">
<div className="col-md-4">
Country
</div>
<div className="col-md-8">
<select name="country"
React Js
value={[Link]}
onChange={[Link]}>
<option value="india">India</option>
<option value="usa">USA</option>
<option value="uk">London</option>
</select>
</div>
</div>
<br></br>
<div className="row">
<div className="col-md-4"></div>
<div className="col-md-8">
disabled={![Link]}
onClick={[Link]}>Register</button>
</div></div>
</div></div>
};
};
OutPut:
React Js
React Js
(MYSQL-MSSQL-MONGODB)
Ex.
[Link]
Queries:
**********************************************
host : localhost
user : root
password : root
database : crud
table : products
********************************************
Ex.
MySQL_CRUD
4) switch to MySQL_CRUD
React Js
> cd MySQL_CRUD
=> mysql
=> cors
=> body-parser
=> express
GET
POST
PUT
DELETE
HEAD
**************************************
MySQL_CRUD
config
db_properties.js
db_connection.js
fetch
[Link]
insert
[Link]
update
[Link]
delete
[Link]
[Link]
***************************************
• "[Link]" file used to create the GET Request. (fetch the data
from products table).
• "[Link]" file used to create the POST Request (insert the
record into products table).
• "[Link]" file used to create the PUT Request (update the
record into products table).
• "[Link]" file used to create the DELETE Request (delete the
record from products table).
db_properties.js
let obj = {
host : "localhost",
user : "root",
password : "root",
database : "crud"
};
[Link] = obj;
db_connection.js
let conn = {
getConnection : ()=>{
return [Link](obj);
}
React Js
};
[Link] = conn;
[Link]
[Link](`select * from
products`,(err,records,fields)=>{
else{
[Link](records);
});
});
[Link] = fetch;
[Link]
(err,result)=>{
else{
[Link]({insert:"success"});
});
});
[Link] = insert;
[Link]
(err,result)=>{
else{
[Link]({update : "success"});
});
React Js
});
[Link] = update;
[Link]
else{
[Link]({delete:"success"});
});
});
[Link] = remove;
[Link]
[Link](cors());
[Link]([Link]());
React Js
[Link]([Link]({extended:false}));
[Link]("/fetch",require("./fetch/fetch"));
[Link]("/insert",require("./insert/insert"));
[Link]("/update",require("./update/update"));
[Link]("/delete",require("./delete/delete"));
[Link](8080);
> cd MySQL_CRUD
*****************************
React Js
-----------------------
MongoDB_CRUD
2) switch to MongoDB_CRUD
> cd MongoDB_CRUD
> mongod
> mongo
> [Link]("products");
> [Link]({"p_id":111,
"p_name":"p_one",
"p_cost":10000});
> [Link]();
React Js
********************************************
host : localhost
port : 27017
protocol : mongodb
database : crud
collection : products
********************************************
=> express
=> cors
=> body-parser
*************************************
MongoDB_CRUD
fetch
[Link]
insert
[Link]
update
[Link]
delete
[Link]
[Link]
*************************************
• [Link] file used to create the GET Request (fetch the data
from products collection)
• [Link] file used to create the POST Request (insert the
record into products collection)
• [Link] file used to create the PUT Request (update the
record present in products collection)
React Js
[Link]
[Link]("mongodb://localhost:27017/crud",(err,db)=>{
else{
[Link]("products").find().toArray((err,array)=>{
else{
[Link](array);
})
});
});
[Link] = fetch;
React Js
[Link]
[Link]("mongodb://localhost:27017/crud",(err,db)=>{
else{
[Link]("products").insertOne({"p_id":[Link].p_id,
"p_name":[Link].p_name,
"p_cost":[Link].p_cost},(err,result)=>{
else{
[Link]({insert : "success"});
});
});
});
[Link] = insert;
React Js
[Link]
[Link]("mongodb://localhost:27017/crud",(err,db)=>{
else{
[Link]("products").updateOne({"p_id":[Link].p_id},
{$set:{"p_name":[Link].p_name,"p_cost":[Link].p_cost}}
(err,result)=>{
else{
[Link]({update : "success"});
});
});
});
React Js
[Link] = update;
[Link]
[Link]("mongodb://localhost:27017/crud",(err,db)=>{
else{
[Link]("products").deleteOne({"p_id":[Link].p_id},(err,
result)=>{
else{
[Link]({delete : "success"});
});
});});
[Link] = remove;
[Link]
[Link](cors());
[Link]([Link]());
[Link]([Link]({extended:false}));
[Link]("/fetch",require("./fetch/fetch"));
[Link]("/insert",require("./insert/insert"));
[Link]("/update",require("./update/update"));
[Link]("/delete",require("./delete/delete"));
[Link](8080);
**********************************
React Js
-------------------------
1) install SQLServer
Ex.
*******************************************
user : sa
password : 123
database : crud
table : products
******************************************
4) switch to MSSQL_CRUD
> cd MSSQL_CRUD
=> express
=> cors
=> body-parser
=> [email protected]
React Js
***********************************
MSSQL_CRUD
config
db_properties.js
fetch
[Link]
insert
[Link]
update
[Link]
delete
[Link]
[Link]
***********************************
db_properties.js
let obj = {
server : "localhost",
React Js
user : "sa",
password : "123",
database : "crud"
};
[Link] = obj;
[Link]
[Link](obj,(err)=>{
else{
[Link](`select * from
products`,(err,records)=>{
else{
[Link]([Link]);
[Link]();
});
}
React Js
});
});
[Link] = fetch;
[Link]
[Link](obj,(err)=>{
else{
'${[Link].p_name}',
${[Link].p_cost})`,(err,result)=>{
else{
[Link]({insert : "success"});
[Link]();
React Js
});
});
});
[Link] = insert;
[Link]
[Link](obj,(err)=>{
else{
(err,result)=>{
else{
[Link]({update : "success"});
[Link]();
React Js
});
});
});
[Link] = update;
[Link]
[Link](obj,(err)=>{
else{
else{
[Link]({delete:"success"});
[Link]();
});
}
React Js
});
});
[Link] = remove;
[Link]
[Link](cors());
[Link]([Link]());
[Link]([Link]({extended:false}));
[Link]("/fetch",require("./fetch/fetch"));
[Link]("/insert",require("./insert/insert"));
[Link]("/update",require("./update/update"));
[Link]("/delete",require("./delete/delete"));
[Link](8080);
> cd MSSQL_CRUD