React Master - Notes - Student copy
React Master - Notes - Student copy
FormatSalary.js
import './FormatSalary.css';
function FormatSalary(salary){
if (salary > 100000){
return <p className='high'>{salary}</p>
}
else if (salary >= 40000){
return <p className='medium'>{salary}</p>
}
else{
return <p className='low'>{salary}</p>
}
}
export default FormatSalary;
Assignment Solution
Display.js
import FormatSalary from './FormatSalary';
function Display(props) {
const salary = FormatSalary(props.user.salary);
return (
<p>
<p>User name is: {props.user.name}</p>
<p>User Salary is: {salary}</p>
<p>User age is: {props.user.age}</p>
</p>
)
}
export default Display;
Assignment Solution
Index.js (for one user only)
const user = {
name: "JECRC Student",
salary: 200000,
age: 20
}
const element = (
<div>
<Display user={user} />
</div>
);
root.render(element);
State management
State is similar to props, but it is private to component and
fully managed and controlled by component itself.
Functional components doesn’t support states, but with
help of hooks we can add states (extra functionality).
Only class components supports states.
React components has a built-in state object.
The state object is where you store property values that
belongs to the component.
When the state object changes, the component re-renders.
Using states in class component
Define state in constructor
constructor(props) {
super(props);
this.state = {key: “value"};
}
Use state
{this.state.key}
We can define multiple state key value pairs.
this.state = {
brand: "Ford",
model: “2023",
color: "red"
};
Props v/s States
Props are immutable i.e. once set the props cannot
be changed, while State is to be used to hold data
that may change over time and to control the
behavior after each change.
States can be used in Class Components,
Functional components with the use of React Hooks
(useState and other methods) while Props don’t
have this limitation.
While Props are set by the parent component, State
is generally updated by event handlers.
Update State
States can’t be updated explicitely like
this.state.attribute = “new-value”
We must have to use setState method like
this.setState({attribute: “new-value”})
Example: (partially code snippet)
this.state = {brand: “Ford”}
changeBrand = () => {this.setState({brand: “blue”});
<p>{this.state.brand}</p>
<button type=“button” onClick={this.changeBrand}
Change color </button>
Initialize state
Update state
Assignment objective
Create a class component. Define 2 separate states
“name” and “salary”. Initial values will be “Demo” and
“100000”.
Show name and salary in separate line with
appropriate prefix text.
Add 2 buttons next to salary “increment” and
“decrement”.
On click on increment button, salary should increased
by 1000 and on click on decrement, salary should
decreased by 1000
Create simple input form
Create a class component and create signup html form
in it which will accept
Name
Password
Submit button
On submit button take following actions
If Name is less than 5 character then show alert
If password is less than 8 character then show alert
If all ok, display name and password in console
Solution
render () {
return (
<form>
<h2>Sign up</h2>
<div>
<label>Name</label>
<input type=”text” name=”name” />
</div>
<div>
<label>Password</label>
<input type=”password” name=”password” />
</div>
<button type=”submit”>
Sign up
</button>
</form>
)
}
Try to run above form and observe reload on submit button click
Validate form using state
Setup states
constructor (props) {
super(props);
this.state = {
name: '',
password: ''
}
}
Bind control with this states
value={this.state.email} and value={this.state.password}
Now observe behaviour when we try to type anything
in text boxes.
Validate form using state
Bind change event
onChange={(event) => this.handleUserInput(event)}
Define handleUserInput method
handleUserInput (e) {
const name = e.target.name;
const value = e.target.value;
this.setState({[name]: value});
console.log(name, value); //Check this
}
function MyComponent() {
const [error, setError] = useState(null);
const [isLoaded, setIsLoaded] = useState(false);
const [items, setItems] = useState([]);
useEffect(() => {
fetch("https://reqres.in/api/users")
.then(res => res.json())
.then(
(result) => {
setIsLoaded(true);
setItems(result.data);
},
(error) => {
setIsLoaded(true);
setError(error);
}
)
}, [])
if (error) {
return <div>Error: {error.message}</div>;
} else if (!isLoaded) {
return <div>Loading...</div>;
} else {
return (
<ul>
{items.map(item => (
<li key={item.id}>
{item.first_name} {item.last_name}
</li>
))}
</ul>
);
}
}
<Outlet />
</>
)
};
function getCookie(cname) {
let name = cname + "=";
let ca = document.cookie.split(';');
for(let i = 0; i < ca.length; i++) {
let c = ca[i];
while (c.charAt(0) == ' ') {
c = c.substring(1);
}
if (c.indexOf(name) == 0) {
return c.substring(name.length, c.length);
}
}
return "";
}
function checkCookie() {
let user = getCookie("username");
if (user != "") {
alert("Welcome again " + user);
} else {
user = prompt("Please enter your name:", "");
if (user != "" && user != null) {
setCookie("username", user, 365);
}
}
}
<body onload="checkCookie()"></body>
What is Redux
Redux is a state management library.
We need to install this package before use.
This is not a required package to state management,
but preferred for medium to big application.
To use redux package install it first:
npm install redux react-redux
It’s extremely lightweight (approximately 2KB in size)
Why use Redux
Props can be passed from one component to another
but read only.
States can’t be passed from one component to another
but writable.
Using combination of props and states we can manage
states, but passing data in child element (deeper
hierarchy) is difficult to maintain.
With Redux we have a centralized store to maintain
state of application.
Without Redux
Redux Architecture
With Redux
Redux explained
There are three core components in Redux:
actions
store
reducers
Redux actions
Redux actions are events.
They are the only way you can send data from your
application to your Redux store. The data can be from user
interactions, API calls, or even form submissions.
Actions are plain JavaScript objects that must have
a type property to indicate the type of action to be carried
out, and
a payload object that contains the information that should
be used to change the state.
Actions are created via an action creator, which in simple
terms is a function that returns an action. And actions are
executed using the store.dispatch() method which sends
the action to the store.
Redux actions continue
Example of action:
{
type: "LOGIN",
payload: {
username: "foo",
password: "bar"
}
}
Example of action creator:
const setLoginStatus = (username, password) => {
return {
type: "LOGIN",
payload: {
username,
password
}
}
}
Redux reducers
Reducers are pure functions that take the current state of an application, perform an action, and return a new state.
Example of reducers:
const LoginComponent = (state = initialState, action) => {
switch (action.type) {
if (user.password == action.password) {
return {
...user,
login_status: "LOGGED IN"
}
}
});
default:
return state;
}
};
Redux store
The store is a “container” (really a JavaScript object)
that holds the application state.
Redux allows individual components connect to the
store and apply changes to it by dispatching actions.
It is highly recommended to keep only one store in any
application.
Example to create a store:
const store = createStore(LoginComponent);
Redux in action - Install
npm install redux react-redux
Redux in action - store
Create a Redux store in a file called store.js:
ReactDOM.render(
<Provider store={store}>
<App />
</Provider>,
document.getElementById('root')
);
Redux in action - connect
Connect your component to the Redux store using the connect function (Counter component as Counter.js)
function mapStateToProps(state) {
return {
count: state.count,
};
}
function mapDispatchToProps(dispatch) {
return {
increment: () => dispatch({ type: 'INCREMENT' }),
decrement: () => dispatch({ type: 'DECREMENT' }),
};
}
Create Redux Store: Next, we create a Redux store using the createStore function from Redux. The
store holds the application's state, and we define a reducer function counterReducer to handle state
updates based on different action types. In this example, we have a simple counter state with an initial
value of 0.
Create React Components: We create a React component called Counter that represents a counter
display with buttons for incrementing and decrementing the counter value. This component uses the
connect function from react-redux to connect to the Redux store and access the counter value from
the Redux state as a prop. We also define mapStateToProps and mapDispatchToProps functions to
map the Redux state and actions to the component props, respectively. mapStateToProps maps the
counter value from the Redux state to the counter prop, and mapDispatchToProps maps the
increment and decrement actions to the corresponding dispatch functions.
Connect Root Component to Redux Store: We create a root component called App that wraps the
Counter component with the Provider component from react-redux. The Provider component makes
the Redux store available to all child components, including Counter, so they can access the Redux
state and dispatch actions.
Render Root Component: Finally, we render the App component in the index.js file using
ReactDOM.render, which renders the App component inside the root element in the HTML file.
Redux in action - Finish
Now you can use the Counter component in your app
and it will be connected to the Redux store. When you
click the "+" or "-" buttons, the increment or
decrement functions will dispatch actions to the store,
causing it to update and trigger a re-render of the
component with the new state.
Testing react app
Software development is incomplete without quality
assurance.
Software testing for an application makes sure that it is
defect-free, saving the cost.
It helps deliver a high-quality product for a better end-
user experience
Selecting the right web development framework is
essential to building a high-quality app.
Jest and react-testing-library are popular framework to
test react apps
Types of test
Unit test
In this kind of test, the goal is to verify that each unit of our application, considered in isolation, is
working correctly. An example would be testing that a particular function returns an expected value, give
some known inputs. We’ll see several examples in this article.
Smoke test
This kind of test is done to check that the system is up and running. For example, in a React app, we
could just render our main app component and call it a day. If it renders correctly we can be fairly certain
that our app would render on the browser.
Integration test
This sort of test is carried out to verify that two or more modules can work well together. For example,
you might run a test to verify that your server and database are actually communicating correctly.
Functional test
A functional test exists to verify that the system meets its functional specification.
End-to-end test
This kind of test involves testing the application the same way it would be used in the real world.
Acceptance test
This is usually done by the business owner to verify that the system meets specifications.
Performance test
This sort of testing is carried out to see how the system performs under significant load. In frontend
development, this is usually about how fast the app loads on the browser.
Test example (Continue Counter example)
Install package
npm install --save-dev @testing-library/react
Create a simplified Counter2.js
import React, { useState } from 'react';
function Counter2() {
const [count, setCount] = useState(0);
function increment() {
setCount(count + 1);
}
return (
<div>
<h1 data-testid="counter">{count}</h1>
<button data-testid="button" onClick={increment}>Increment</button>
</div>
);
}
expect(counterElement.textContent).toBe('0');
fireEvent.click(buttonElement);
expect(counterElement.textContent).toBe('1');
});
Run tests:
npm test
Test output
We can notice error in App component, because we
modified App component entirely.
Delete App.test.js
Run “npm test” again
DevTools
Developer tools
A very useful tool, if you are working on React.js
applications
It helps you to inspect and edit the React component
tree that builds the page, and for each component, one
can check the props, the state, hooks, etc.
This tool also helps you to know if a particular
application React.js has been used or not.
It’s available for chrome and firefox
Download and use DevTools
Add chrome extension from:
https://chrome.google.com/webstore/detail/react-
developer-
tools/fmkadmapgofadopljbjfkapdkoienihi?hl=en
Re launch chrome
Right click and select inspect
You will see “Components” and “Profile” 2 options
Show demo
CREATE UI for CRUD operations