React Notes
React Notes
React is a front-end and open-source JavaScript library which is useful in developing user
interfaces specifically for applications with a single page. It is helpful in building complex
and reusable user interface (UI) components of mobile and web applications as it follows the
component-based approach.
Use of Virtual DOM to improve efficiency: React uses virtual DOM to render the view. As the name
suggests, virtual DOM is a virtual representation of the real DOM. Each time the data changes in a
react app, a new virtual DOM gets created. Creating a virtual DOM is much faster than rendering the
UI inside the browser. Therefore, with the use of virtual DOM, the efficiency of the app improves.
Gentle learning curve: React has a gentle learning curve when compared to frameworks like
Angular. Anyone with little knowledge of javascript can start building web applications using React.
SEO friendly: React allows developers to develop engaging user interfaces that can be easily
navigated in various search engines. It also allows server-side rendering, which boosts the SEO of an
app.
Reusable components: React uses component-based architecture for developing applications.
Components are independent and reusable bits of code. These components can be shared across
various applications having similar functionality. The re-use of components increases the pace of
development.
Huge ecosystem of libraries to choose from: React provides you with the freedom to choose the
tools, libraries, and architecture for developing an application based on your requirement.
The useState() is a built-in React Hook that allows you for having state variables in
functional components. It should be used when the DOM has something that is dynamically
manipulating/controlling.
In the below-given example code, The useState(0) will return a tuple where the count is the
first parameter that represents the counter’s current state and the second parameter setCounter
method will allow us to update the state of the counter.
...
const [count, setCounter] = useState(0);
const [otherStuffs, setOtherStuffs] = useState(...);
...
const setCount = () => {
setCounter(count + 1);
setOtherStuffs(...);
...
};
We can make use of setCounter() method for updating the state of count anywhere. In this
example, we are using setCounter() inside the setCount function where various other things
can also be done. The idea with the usage of hooks is that we will be able to keep our code
more functional and avoid class-based components if they are not required.
A key is a special string attribute that needs to be included when using lists of elements.
Keys help react identify which elements were added, changed or removed.
Keys should be given to array elements for providing a unique identity for each element.
Without keys, React does not understand the order or uniqueness of each element.
With keys, React has an idea of which particular element was deleted, edited, and added.
Keys are generally used for displaying a list of data coming from an API.
6. What is JSX?
JSX stands for JavaScript XML. It allows us to write HTML inside JavaScript and place them
in the DOM without using functions like appendChild( ) or createElement( ).
As stated in the official docs of React, JSX provides syntactic sugar for
React.createElement( ) function.
Without using JSX, we would have to create an element by the following process:
const container = (
<div>
<p>This is a text</p>
</div>
);
ReactDOM.render(container,rootElement);
As one can see in the code above, we are directly using HTML inside JavaScript.
Before the introduction of Hooks in React, functional components were called stateless
components and were behind class components on a feature basis. After the introduction of
Hooks, functional components are equivalent to class components.
Although functional components are the new trend, the react team insists on keeping class
components in React. Therefore, it is important to know how these components differ.
Declaration
Functional components are nothing but JavaScript functions and therefore can be declared
using an arrow function or the function keyword:
function card(props){
return(
<div className="main-container">
<h2>Title of the card</h2>
</div>
)
}
const card = (props) =>{
return(
<div className="main-container">
<h2>Title of the card</h2>
</div>
)
}
Class components, on the other hand, are declared using the ES6 class:
Handling props
Let’s render the following component with props and analyse how functional and class
components handle props:
In functional components, the handling of props is pretty straightforward. Any prop provided
as an argument to a functional component can be directly used inside HTML elements:
function StudentInfo(props){
return(
<div className="main">
<h2>{props.name}</h2>
<h4>{props.rollNumber}</h4>
</div>
)
}
As we can see in the code above, this keyword is used in the case of class components.
Handling state
Functional components use React hooks to handle state. It uses the useState hook to set the
state of a variable inside the component:
function ClassRoom(props){
let [studentsCount,setStudentsCount] = useState(0);
const addStudent = () => {
setStudentsCount(++studentsCount);
}
return(
<div>
<p>Number of students in class room: {studentsCount}</p>
<button onClick={addStudent}>Add Student</button>
</div>
)
}
Since useState hook returns an array of two items, the first item contains the current state, and
the second item is a function used to update the state.
In the code above, using array destructuring we have set the variable name to studentsCount
with a current value of “0” and setStudentsCount is the function that is used to update the
state.
For reading the state, we can see from the code above, the variable name can be directly used
to read the current state of the variable.
We cannot use React Hooks inside class components, therefore state handling is done very
differently in a class component:
Let’s take the same above example and convert it into a class component:
this.addStudent = this.addStudent.bind(this);
}
addStudent(){
this.setState((prevState)=>{
return {studentsCount: prevState.studentsCount++}
});
}
render(){
return(
<div>
<p>Number of students in class room:
{this.state.studentsCount}</p>
<button onClick={this.addStudent}>Add Student</button>
</div>
)
}
}
In the code above, we see we are using this.state to add the variable studentsCount and
setting the value to “0”.
For updating the state, we need to first bind the addStudent function to this. Only then, we
will be able to use the setState function which is used to update the state.
8. What is the virtual DOM? How does react use the virtual DOM to
render the UI?
DOM manipulation is an integral part of any web application, but DOM manipulation
is quite slow when compared to other operations in JavaScript. The efficiency of
the application gets affected when several DOM manipulations are being done.
Most JavaScript frameworks update the entire DOM even when a small part of the
DOM changes.
For example, consider a list that is being rendered inside the DOM. If one of the
items in the list changes, the entire list gets rendered again instead of just
rendering the item that was changed/updated. This is called inefficient updating.
To address the problem of inefficient updating, the react team introduced the
concept of virtual DOM.
**Note- One may think updating every virtual DOM object might be inefficient, but
that’s not the case. Updating the virtual DOM is much faster than updating the real
DOM since we are just updating the blueprint of the real DOM.
React uses two virtual DOMs to render the user interface. One of them is used to
store the current state of the objects and the other to store the previous state of the
objects. Whenever the virtual DOM gets updated, react compares the two virtual
DOMs and gets to know about which virtual DOM objects were updated. After
knowing which objects were updated, react renders only those objects inside the
real DOM instead of rendering the complete real DOM. This way, with the use of
virtual DOM, react solves the problem of inefficient updating.
✔️ ✔️ ✔️
One-time value retrieval (e.g. on submit)
✔️ ✔️ ✔️
Validating on submit
❌ ✔️ ✔️
Field-level Validation
❌ ✔️ ✔️
Conditionally disabling submit button
❌ ✔️ ✔️
Enforcing input format
❌ ✔️ ✔️
several inputs for one piece of data
❌ ✔️ 🤔
dynamic inputs
When a user enters data inside the input element of a controlled component,
onChange function gets triggered and inside the code, we check whether the value
entered is valid or invalid. If the value is valid, we change the state and re-render
the input element with the new value.
function FormValidation(props) {
let [inputValue, setInputValue] = useState("");
let updateInput = e => {
setInputValue(e.target.value);
};
return (
<div>
<form>
<input type="text" value={inputValue} onChange={updateInput} />
</form>
</div>
);
}
As one can see in the code above, the value of the input element is determined by
the state of the inputValue variable. Any changes made to the input element is
handled by the updateInput function.
The state of the input element is handled by the DOM. Whenever the value of the
input element is changed, event-based callbacks are not called. Basically, react
does not perform any action when there are changes made to the input element.
Whenever use enters data inside the input field, the updated data is shown directly.
To access the value of the input element, we can use ref.
function FormValidation(props) {
let inputValue = React.createRef();
let handleSubmit = e => {
alert(`Input value: ${inputValue.current.value}`);
e.preventDefault();
};
return (
<div>
<form onSubmit={handleSubmit}>
<input type="text" ref={inputValue} />
<button type="submit">Submit</button>
</form>
</div>
);
}
As one can see in the code above, we are not using onChange function to govern
the changes made to the input element. Instead, we are using ref to access the
value of the input element.
The props in React are the inputs to a component of React. They can be single-
valued or objects having a set of values that will be passed to components of React
during creation by using a naming convention that almost looks similar to HTML-tag
attributes. We can say that props are the data passed from a parent component
into a child component.
For example, consider we are creating an element with reactProp property as given
below: <Element reactProp = "1" />
This reactProp name will be considered as a property attached to the native props
object of React which already exists on each component created with the help of
React library: props.reactProp;.
Props State
React State
Every component in react has a built-in state object, which contains all the property values
that belong to that component.
In other words, the state object controls the behaviour of a component. Any change in the
property values of the state object leads to the re-rendering of the component.
Note- State object is not available in functional components but, we can use React
Hooks to add state to a functional component.
How to declare a state object?
Example:
As one can see in the code above, we can use the state by
calling this.state.propertyName and we can change the state object property
using setState method.
React Props
Every React component accepts a single object argument called props (which
stands for “properties”). These props can be passed to a component using HTML
attributes and the component accepts these props as an argument.
<Car brand="Mercedes"/>
The component receives the props:
In Class component:
In Functional component:
function Car(props) {
let [brand, setBrand] = useState(props.brand);
}
There are two types of side effects in React component. They are:
Effects without Cleanup: This side effect will be used in useEffect which does not restrict
the browser from screen update. It also improves the responsiveness of an application. A
few common examples are network requests, Logging, manual DOM mutations, etc.
Effects with Cleanup: Some of the Hook effects will require the cleanup after updating of
DOM is done. For example, if you want to set up an external data source subscription, it
requires cleaning up the memory else there might be a problem of memory leak. It is a
known fact that React will carry out the cleanup of memory when the unmounting of
components happens. But the effects will run for each render() method rather than for any
specific method. Thus we can say that, before execution of the effects succeeding time the
React will also cleanup effects from the preceding render.
4.5
100K+
Play Store
The disadvantage of using prop drilling is that the components that should
otherwise be not aware of the data have access to the data.
Any component which uses one of the following lifecycle methods is considered an
error boundary.
In what places can an error boundary detect an error?
1. Render phase
2. Inside a lifecycle method
3. Inside the constructor
In the code above, when the counterValue equals 2, we throw an error inside the
render method.
When we are not using the error boundary, instead of seeing an error, we see a
blank page. Since any error inside the render method leads to unmounting of the
component. To display an error that occurs inside the render method, we use error
boundaries.
Now with the error boundary, we can render the CounterComponent in the
following way:
<ErrorBoundary>
<CounterComponent/>
</ErrorBoundary>
React Hooks are the built-in functions that permit developers for using the state
and lifecycle methods within React components. These are newly added features
made available in React 16.8 version. Each lifecycle of a component is having 3
phases which include mount, unmount, and update. Along with that, components
have properties and states. Hooks will allow using these methods by developers for
improving the reuse of code with higher flexibility navigating the component tree.
Using Hook, all features of React can be used without writing class
components. For example, before React version 16.8, it required a class
component for managing the state of a component. But now using the useState
hook, we can keep the state in a functional component.
What are Hooks? Hooks are functions that let us “hook into” React state and
lifecycle features from a functional component.
React Hooks cannot be used in class components. They let us write components
without class.
React hooks were introduced in the 16.8 version of React. Previously, functional
components were called stateless components. Only class components were used
for state management and lifecycle methods. The need to change a functional
component to a class component, whenever state management or lifecycle
methods were to be used, led to the development of Hooks.
function Person(props) {
// We are declaring a state variable called name.
// setName is a function to update/change the value of name
let [name, setName] = useState('');
}
The state variable “name” can be directly used inside the HTML.
17. What are the rules that must be followed while using React
Hooks?
There are 2 rules which must be followed while you code with Hooks:
React Hooks must be called only at the top level. It is not allowed to call them inside the
nested functions, loops, or conditions.
It is allowed to call the Hooks only from the React Function Components.
The useEffect React Hook is used for performing the side effects in functional
components. With the help of useEffect, you will inform React that your component
requires something to be done after rendering the component or after a state
change. The function you have passed(can be referred to as “effect”) will be
remembered by React and call afterwards the performance of DOM updates is
over. Using this, we can perform various calculations such as data fetching, setting
up document title, manipulating DOM directly, etc, that don’t target the output
value. The useEffect hook will run by default after the first render and also after
each update of the component. React will guarantee that the DOM will be updated
by the time when the effect has run by it.
Where the first argument callback represents the function having the logic of side-
effect and it will be immediately executed after changes were being pushed to
DOM. The second argument dependencies represent an optional array of
dependencies. The useEffect() will execute the callback only if there is a change in
dependencies in between renderings.
Example:
The above code will update the document title which is considered to be a side-
effect as it will not calculate the component output directly. That is why updating of
document title has been placed in a callback and provided to useEffect().
Consider you don’t want to execute document title update each time on rendering
of WelcomeGreetings component and you want it to be executed only when the
name prop changes then you need to supply name as a dependency
to useEffect(callback, [name]).
Earlier, refs were only limited to class components but now it can also be
accessible in function components through the useRef Hook in React.
A Custom Hook is a function in Javascript whose name begins with ‘use’ and which
calls other hooks. It is a part of React v16.8 hook update and permits you for
reusing the stateful logic without any need for component hierarchy restructuring.
In almost all of the cases, custom hooks are considered to be sufficient for
replacing render props and HoCs (Higher-Order components) and reducing the
amount of nesting required. Custom Hooks will allow you for avoiding multiple
layers of abstraction or wrapper hell that might come along with Render Props and
HoCs.
function App() {
return (
<React.StrictMode>
<div classname="App">
<Header/>
<div>
Page Content
</div>
<Footer/>
</div>
</React.StrictMode>
);
}
The Parent component is the parent component and the Message is the child component.
Any change in the parent component will lead to re-rendering of the child component as
well. To prevent the re-rendering of child components, we use the
shouldComponentUpdate( ) method:
**Note- Use shouldComponentUpdate( ) method only when you are sure that it’s a
static component.
As one can see in the code above, we have returned false from the
shouldComponentUpdate( ) method, which prevents the child component from re-
rendering.
There are many different ways through which one can style a React component.
Some of the ways are :
Inline Styling: We can directly style an element using inline style attributes. Make sure the
value of style is a JavaScript object:
Using JavaScript object: We can create a separate JavaScript object and set the desired
style properties. This object can be used as the value of the inline style attribute.
headingStyles = {
color: "blue",
fontSize: "48px"
};
render() {
return (
<div>
<h3 style={this.headingStyles}>This is a heading</h3>
<p style={this.paragraphStyles}>This is a paragraph</p>
</div>
);
}
}
CSS Stylesheet: We can create a separate CSS file and write all the styles for the
component inside that file. This file needs to be imported inside the component file.
import './RandomComponent.css';
CSS Modules: We can create a separate CSS module and import this module inside our
component. Create a file with “.module.css”‘ extension, styles.module.css:
.paragraph{
color:"red";
border:1px solid black;
}
We can import this file inside the component and use it:
There are many ways through which one can optimize the performance of a React
app, let’s have a look at some of them:
Using useMemo( ) -
o It is a React hook that is used for caching CPU-Expensive functions.
o Sometimes in a React app, a CPU-Expensive function gets called repeatedly due to re-
renders of a component, which can lead to slow rendering.
useMemo( ) hook can be used to cache such functions. By using useMemo( ), the CPU-
Expensive function gets called only when it is needed.
Using React.PureComponent -
o It is a base component class that checks the state and props of a component to know
whether the component should be updated.
o Instead of using the simple React.Component, we can use React.PureComponent to
reduce the re-renders of a component unnecessarily.
Maintaining State Colocation -
o This is a process of moving the state as close to where you need it as possible.
o Sometimes in React app, we have a lot of unnecessary states inside the parent
component which makes the code less readable and harder to maintain. Not to forget,
having many states inside a single component leads to unnecessary re-renders for the
component.
o It is better to shift states which are less valuable to the parent component, to a separate
component.
Lazy Loading -
o It is a technique used to reduce the load time of a React app. Lazy loading helps reduce
the risk of web app performances to a minimum.
With the help of props, we can send data from a parent to a child component.
How do we do this?
return (
<div>
<button onClick={increment}>Increment Counter</button>
<ChildComponent counterValue={counter} />
</div>
);
}
As one can see in the code above, we are rendering the child component inside the
parent component, by providing a prop called counterValue. The value of the
counter is being passed from the parent to the child component.
We can use the data passed by the parent component in the following way:
function ChildComponent(props) {
return (
<div>
<p>Value of counter: {props.counterValue}</p>
</div>
);
}
Create a callback in the parent component which takes in the data needed as a parameter.
Pass this callback as a prop to the child component.
Send data from the child component using the callback.
We are considering the same example above but in this case, we are going to pass
the updated counterValue from child to parent.
Step1 and Step2: Create a callback in the parent component, pass this callback as
a prop.
function ParentComponent(props) {
let [counter, setCounter] = useState(0);
let callback = valueFromChild => setCounter(valueFromChild);
return (
<div>
<p>Value of counter: {counter}</p>
<ChildComponent callbackFunc={callback} counterValue={counter} />
</div>
);
}
As one can see in the code above, we created a function called callback which
takes in the data received from the child component as a parameter.
Next, we passed the function callback as a prop to the child component.
function ChildComponent(props) {
let childCounterValue = props.counterValue;
return (
<div>
<button onClick={() => props.callbackFunc(++childCounterValue)}>
Increment Counter
</button>
</div>
);
}
In the code above, we have used the props.counterValue and set it to a variable
called childCounterValue.
This way, we can pass data from the child to the parent component.
While developing React applications, we might develop components that are quite
similar to each other with minute differences. In most cases, developing similar
components might not be an issue but, while developing larger applications we
need to keep our code DRY, therefore, we want an abstraction that allows us to
define this logic in a single place and share it across components. HOC allows us
to create that abstraction.
Example of a HOC:
Notice the above components, both have similar functionality but, they are calling
different methods to an API endpoint.
In the code above, we have created a function called HOC which returns a
component and performs functionality that can be shared across both
the ArticlesList component and UsersList Component.
The second parameter in the HOC function is the function that calls the method on
the API endpoint.
There are four different phases in the lifecycle of React component. They are:
Initialization: During this phase, React component will prepare by setting up the default
props and initial state for the upcoming tough journey.
Mounting: Mounting refers to putting the elements into the browser DOM. Since React
uses VirtualDOM, the entire browser DOM which has been currently rendered would not
be refreshed. This phase includes the lifecycle
methods componentWillMount and componentDidMount.
Updating: In this phase, a component will be updated when there is a change in the state
or props of a component. This phase will have lifecycle methods
like componentWillUpdate, shouldComponentUpdate, render,
and componentDidUpdate.
Unmounting: In this last phase of the component lifecycle, the component will be
removed from the DOM or will be unmounted from the browser DOM. This phase will have
the lifecycle method named componentWillUnmount.
8. What are the lifecycle methods of React?
React lifecycle hooks will have the methods that will be automatically called at
different phases in the component lifecycle and thus it provides good control over
what happens at the invoked point. It provides the power to effectively control and
manipulate what goes on throughout the component lifecycle.
For example, if you are developing the YouTube application, then the application
will make use of a network for buffering the videos and it consumes the power of
the battery (assume only these two). After playing the video if the user switches to
any other application, then you should make sure that the resources like network
and battery are being used most efficiently. You can stop or pause the video
buffering which in turn stops the battery and network usage when the user switches
to another application after video play.
So we can say that the developer will be able to produce a quality application with
the help of lifecycle methods and it also helps developers to make sure to plan
what and how to do it at different points of birth, growth, or death of user interfaces.
constructor(): This method will be called when the component is initiated before
anything has been done. It helps to set up the initial state and initial values.
getDerivedStateFromProps() : This method will be called just before element(s)
rendering in the DOM. It helps to set up the state object depending on the initial props. The
getDerivedStateFromProps() method will have a state as an argument and it returns an
object that made changes to the state. This will be the first method to be called on an
updating of a component.
render(): This method will output or re-render the HTML to the DOM with new changes.
The render() method is an essential method and will be called always while the remaining
methods are optional and will be called only if they are defined.
componentDidMount(): This method will be called after the rendering of the component.
Using this method, you can run statements that need the component to be already kept in
the DOM.
shouldComponentUpdate() : The Boolean value will be returned by this method which will
specify whether React should proceed further with the rendering or not. The default value
for this method will be True.
getSnapshotBeforeUpdate() : This method will provide access for the props as well as
for the state before the update. It is possible to check the previously present value before
the update, even after the update.
componentDidUpdate(): This method will be called after the component has been
updated in the DOM.
componentWillUnmount() : This method will be called when the component removal from
the DOM is about to happen.
Static typing refers to the process of code check during the time of compilation for
ensuring all variables will be statically typed. React Hooks are functions that are
designed to make sure about all attributes must be statically typed. For enforcing
stricter static typing within our code, we can make use of the React API with
custom Hooks.
1. Built-in Hooks: The built-in Hooks are divided into 2 parts as given below:
Basic Hooks:
o useState(): This functional component is used to set and retrieve the state.
o useEffect(): It enables for performing the side effects in the functional components.
o useContext(): It is used for creating common data that is to be accessed by the
components hierarchy without having to pass the props down to each level.
Additional Hooks:
o useReducer() : It is used when there is a complex state logic that is having several sub-
values or when the upcoming state is dependent on the previous state. It will also enable
you to optimization of component performance that will trigger deeper updates as it is
permitted to pass the dispatch down instead of callbacks.
o useMemo() : This will be used for recomputing the memoized value when there is a change
in one of the dependencies. This optimization will help for avoiding expensive calculations
on each render.
o useCallback() : This is useful while passing callbacks into the optimized child
components and depends on the equality of reference for the prevention of unneeded
renders.
o useImperativeHandle(): It will enable modifying the instance that will be passed with
the ref object.
o useDebugValue(): It is used for displaying a label for custom hooks in React DevTools.
o useRef() : It will permit creating a reference to the DOM element directly within the
functional component.
o useLayoutEffect(): It is used for the reading layout from the DOM and re-rendering
synchronously.
It will not require a declaration of any kind of It is necessary to declare the constructor inside the
constructor. class component.
React Hooks can be helpful in implementing Because of the long setup of state declarations,
Redux and context API. class states are generally not preferred.
React Hooks will avoid a lot of overheads such as the instance creation, binding of events,
etc., that are present with classes.
Hooks in React will result in smaller component trees since they will be avoiding the
nesting that exists in HOCs (Higher Order Components) and will render props which result
in less amount of work to be done by React.
Our goal is for Hooks to cover all the functionalities for classes at its earliest. There
are no Hook equivalents for the following methods that are not introduced in Hooks
yet:
getSnapshotBeforeUpdate()
getDerivedStateFromError()
componentDidCatch()
Since it is an early time for Hooks, few third-party libraries may not be compatible
with Hooks at present, but they will be added soon.
React Router refers to the standard library used for routing in React. It permits us
for building a single-page web application in React with navigation without even
refreshing the page when the user navigates. It also allows to change the browser
URL and will keep the user interface in sync with the URL. React Router will make
use of the component structure for calling the components, using which appropriate
information can be shown. Since React is a component-based framework, it’s not
necessary to include and use this package. Any other compatible routing library
would also work with React.
BrowserRouter: It is a router implementation that will make use of the HTML5 history API
(pushState, popstate, and event replaceState) for keeping your UI to be in sync with the
URL. It is the parent component useful in storing all other components.
Routes: It is a newer component that has been introduced in the React v6 and an upgrade
of the component.
Route: It is considered to be a conditionally shown component and some UI will be
rendered by this whenever there is a match between its path and the current URL.
Link: It is useful in creating links to various routes and implementing navigation all over
the application. It works similarly to the anchor tag in HTML.
The React Hook cannot be considered as a replacement for Redux (It is an open-
source, JavaScript library useful in managing the application state) when it comes
to the management of the global application state tree in large complex
applications, even though the React will provide a useReducer hook that manages
state transitions similar to Redux. Redux is very useful at a lower level of
component hierarchy to handle the pieces of a state which are dependent on each
other, instead of a declaration of multiple useState hooks.
Conditional rendering refers to the dynamic output of user interface markups based
on a condition state. It works in the same way as JavaScript conditions. Using
conditional rendering, it is possible to toggle specific application functions, API data
rendering, hide or show elements, decide permission levels, authentication
handling, and so on.
Using if-else conditional logic which is suitable for smaller as well as for medium-sized
applications
Using ternary operators, which takes away some amount of complication from if-else
statements
Using element variables, which will enable us to write cleaner code.
I will assume that you are having some coding knowledge about JavaScript and
have installed Node on your system for creating a below given React Hook
program. An installation of Node comes along with the command-line tools: npm
and npx, where npm is useful to install the packages into a project and npx is
useful in running commands of Node from the command line. The npx looks in the
current project folder for checking whether a command has been installed there.
When the command is not available on your computer, the npx will look in the
npmjs.com repository, then the latest version of the command script will be loaded
and will run without locally installing it. This feature is useful in creating a skeleton
React application within a few key presses.
Open the Terminal inside the folder of your choice, and run the following command:
JSX will permit you for writing HTML-style template syntax directly into the
JavaScript file. This mixture of JavaScript and HTML will be converted by React
toolchain into pure JavaScript that will render the HTML element.
It is possible to define your own React components by writing a function that will
return a JSX element. You can try this by creating a new
file src/SearchItem.jsand put the following code into it.
Now, from the logo.svg, import will be removed and then contents of returned value
in the function App() will be replaced with the following code:
<div className="App">
<header>
Items with Hooks
</header>
<SearchItem/>
</div>
You can notice that the element <SearchItem/> has been used just similar to an
HTML element. The JSX syntax will enable for including the components in this
approach directly within the JavaScript code. Your application can be tested by
running the below-given command in your terminal.
npm start
This command will compile your application and open your default browser
into http://localhost:4000. This command can be kept on running when code
development is in progress to make sure that the application is up-to-date, and also
this browser page will be reloaded each time you modify and save the code.
This application will work finely, but it doesn’t look nice as it doesn’t react to any
input from the user. You can make it more interactive by adding a state with React
Hooks, adding authentication, etc.
A switching component refers to a component that will render one of the multiple
components. We should use an object for mapping prop values to components.
A below-given example will show you how to display different pages based on page
prop using switching component:
Using the below-given code, we can render the view when the browser is resized.
componentWillMount() {
this.updateDimension()
}
componentDidMount() {
window.addEventListener('resize', this.updateDimension)
}
componentWillUnmount() {
window.removeEventListener('resize', this.updateDimension)
}
updateDimension() {
this.setState({width: window.innerWidth, height: window.innerHeight})
}
render() {
return <span>{this.state.width} x {this.state.height}</span>
}
}
Passing data between sibling components of React is possible using React Router
with the help of history.push and match.params.
In the code given below, we have a Parent component AppDemo.js and have two
Child Components HomePage and AboutPage. Everything is kept inside a Router by
using React-router Route. It is also having a route for /about/{params} where we
will pass the data.
Also, the functional component AboutPage will obtain the data passed
by props.match.params.aboutId.
After button click in the HomePage the page will look like below:
21. How to perform automatic redirect after login?
The react-router package will provide the component <Redirect> in React Router.
Rendering of a <Redirect> component will navigate to a newer location. In the
history stack, the current location will be overridden by the new location just like the
server-side redirects.
8. What is the virtual DOM? How does react use the virtual DOM to
render the UI?
DOM manipulation is an integral part of any web application, but DOM manipulation
is quite slow when compared to other operations in JavaScript. The efficiency of
the application gets affected when several DOM manipulations are being done.
Most JavaScript frameworks update the entire DOM even when a small part of the
DOM changes.
For example, consider a list that is being rendered inside the DOM. If one of the
items in the list changes, the entire list gets rendered again instead of just
rendering the item that was changed/updated. This is called inefficient updating.
To address the problem of inefficient updating, the react team introduced the
concept of virtual DOM.
**Note- One may think updating every virtual DOM object might be inefficient, but
that’s not the case. Updating the virtual DOM is much faster than updating the real
DOM since we are just updating the blueprint of the real DOM.
React uses two virtual DOMs to render the user interface. One of them is used to
store the current state of the objects and the other to store the previous state of the
objects. Whenever the virtual DOM gets updated, react compares the two virtual
DOMs and gets to know about which virtual DOM objects were updated. After
knowing which objects were updated, react renders only those objects inside the
real DOM instead of rendering the complete real DOM. This way, with the use of
virtual DOM, react solves the problem of inefficient updating.
Validating on submit
✔️ ✔️ ✔️
❌
Field-level Validation
✔️ ✔️
❌
Conditionally disabling submit button
✔️ ✔️
❌
Enforcing input format
✔️ ✔️
❌
several inputs for one piece of data
✔️ ✔️
❌ 🤔
dynamic inputs
✔️
When a user enters data inside the input element of a controlled component,
onChange function gets triggered and inside the code, we check whether the value
entered is valid or invalid. If the value is valid, we change the state and re-render
the input element with the new value.
function FormValidation(props) {
let [inputValue, setInputValue] = useState("");
let updateInput = e => {
setInputValue(e.target.value);
};
return (
<div>
<form>
<input type="text" value={inputValue} onChange={updateInput} />
</form>
</div>
);
}
As one can see in the code above, the value of the input element is determined by
the state of the inputValue variable. Any changes made to the input element is
handled by the updateInput function.
The state of the input element is handled by the DOM. Whenever the value of the
input element is changed, event-based callbacks are not called. Basically, react
does not perform any action when there are changes made to the input element.
Whenever use enters data inside the input field, the updated data is shown directly.
To access the value of the input element, we can use ref.
function FormValidation(props) {
let inputValue = React.createRef();
let handleSubmit = e => {
alert(`Input value: ${inputValue.current.value}`);
e.preventDefault();
};
return (
<div>
<form onSubmit={handleSubmit}>
<input type="text" ref={inputValue} />
<button type="submit">Submit</button>
</form>
</div>
);
}
As one can see in the code above, we are not using onChange function to govern
the changes made to the input element. Instead, we are using ref to access the
value of the input element.
The props in React are the inputs to a component of React. They can be single-
valued or objects having a set of values that will be passed to components of React
during creation by using a naming convention that almost looks similar to HTML-tag
attributes. We can say that props are the data passed from a parent component
into a child component.
For example, consider we are creating an element with reactProp property as given
below: <Element reactProp = "1" />
This reactProp name will be considered as a property attached to the native props
object of React which already exists on each component created with the help of
React library: props.reactProp;.
Props State
React State
Every component in react has a built-in state object, which contains all the property values
that belong to that component.
In other words, the state object controls the behaviour of a component. Any change in the
property values of the state object leads to the re-rendering of the component.
Note- State object is not available in functional components but, we can use React
Hooks to add state to a functional component.
How to declare a state object?
Example:
As one can see in the code above, we can use the state by
calling this.state.propertyName and we can change the state object property
using setState method.
React Props
Every React component accepts a single object argument called props (which
stands for “properties”). These props can be passed to a component using HTML
attributes and the component accepts these props as an argument.
<Car brand="Mercedes"/>
The component receives the props:
In Class component:
In Functional component:
function Car(props) {
let [brand, setBrand] = useState(props.brand);
}
There are two types of side effects in React component. They are:
Effects without Cleanup: This side effect will be used in useEffect which does not restrict
the browser from screen update. It also improves the responsiveness of an application. A
few common examples are network requests, Logging, manual DOM mutations, etc.
Effects with Cleanup: Some of the Hook effects will require the cleanup after updating of
DOM is done. For example, if you want to set up an external data source subscription, it
requires cleaning up the memory else there might be a problem of memory leak. It is a
known fact that React will carry out the cleanup of memory when the unmounting of
components happens. But the effects will run for each render() method rather than for any
specific method. Thus we can say that, before execution of the effects succeeding time the
React will also cleanup effects from the preceding render.
4.5
100K+
Play Store
The disadvantage of using prop drilling is that the components that should
otherwise be not aware of the data have access to the data.
Any component which uses one of the following lifecycle methods is considered an
error boundary.
In what places can an error boundary detect an error?
1. Render phase
2. Inside a lifecycle method
3. Inside the constructor
In the code above, when the counterValue equals 2, we throw an error inside the
render method.
When we are not using the error boundary, instead of seeing an error, we see a
blank page. Since any error inside the render method leads to unmounting of the
component. To display an error that occurs inside the render method, we use error
boundaries.
Now with the error boundary, we can render the CounterComponent in the
following way:
<ErrorBoundary>
<CounterComponent/>
</ErrorBoundary>
React Hooks are the built-in functions that permit developers for using the state
and lifecycle methods within React components. These are newly added features
made available in React 16.8 version. Each lifecycle of a component is having 3
phases which include mount, unmount, and update. Along with that, components
have properties and states. Hooks will allow using these methods by developers for
improving the reuse of code with higher flexibility navigating the component tree.
Using Hook, all features of React can be used without writing class
components. For example, before React version 16.8, it required a class
component for managing the state of a component. But now using the useState
hook, we can keep the state in a functional component.
What are Hooks? Hooks are functions that let us “hook into” React state and
lifecycle features from a functional component.
React Hooks cannot be used in class components. They let us write components
without class.
React hooks were introduced in the 16.8 version of React. Previously, functional
components were called stateless components. Only class components were used
for state management and lifecycle methods. The need to change a functional
component to a class component, whenever state management or lifecycle
methods were to be used, led to the development of Hooks.
function Person(props) {
// We are declaring a state variable called name.
// setName is a function to update/change the value of name
let [name, setName] = useState('');
}
The state variable “name” can be directly used inside the HTML.
17. What are the rules that must be followed while using React
Hooks?
There are 2 rules which must be followed while you code with Hooks:
React Hooks must be called only at the top level. It is not allowed to call them inside the
nested functions, loops, or conditions.
It is allowed to call the Hooks only from the React Function Components.
The useEffect React Hook is used for performing the side effects in functional
components. With the help of useEffect, you will inform React that your component
requires something to be done after rendering the component or after a state
change. The function you have passed(can be referred to as “effect”) will be
remembered by React and call afterwards the performance of DOM updates is
over. Using this, we can perform various calculations such as data fetching, setting
up document title, manipulating DOM directly, etc, that don’t target the output
value. The useEffect hook will run by default after the first render and also after
each update of the component. React will guarantee that the DOM will be updated
by the time when the effect has run by it.
Where the first argument callback represents the function having the logic of side-
effect and it will be immediately executed after changes were being pushed to
DOM. The second argument dependencies represent an optional array of
dependencies. The useEffect() will execute the callback only if there is a change in
dependencies in between renderings.
Example:
The above code will update the document title which is considered to be a side-
effect as it will not calculate the component output directly. That is why updating of
document title has been placed in a callback and provided to useEffect().
Consider you don’t want to execute document title update each time on rendering
of WelcomeGreetings component and you want it to be executed only when the
name prop changes then you need to supply name as a dependency
to useEffect(callback, [name]).
Earlier, refs were only limited to class components but now it can also be
accessible in function components through the useRef Hook in React.
A Custom Hook is a function in Javascript whose name begins with ‘use’ and which
calls other hooks. It is a part of React v16.8 hook update and permits you for
reusing the stateful logic without any need for component hierarchy restructuring.
In almost all of the cases, custom hooks are considered to be sufficient for
replacing render props and HoCs (Higher-Order components) and reducing the
amount of nesting required. Custom Hooks will allow you for avoiding multiple
layers of abstraction or wrapper hell that might come along with Render Props and
HoCs.
function App() {
return (
<React.StrictMode>
<div classname="App">
<Header/>
<div>
Page Content
</div>
<Footer/>
</div>
</React.StrictMode>
);
}
The Parent component is the parent component and the Message is the child component.
Any change in the parent component will lead to re-rendering of the child component as
well. To prevent the re-rendering of child components, we use the
shouldComponentUpdate( ) method:
**Note- Use shouldComponentUpdate( ) method only when you are sure that it’s a
static component.
As one can see in the code above, we have returned false from the
shouldComponentUpdate( ) method, which prevents the child component from re-
rendering.
There are many different ways through which one can style a React component.
Some of the ways are :
Inline Styling: We can directly style an element using inline style attributes. Make sure the
value of style is a JavaScript object:
Using JavaScript object: We can create a separate JavaScript object and set the desired
style properties. This object can be used as the value of the inline style attribute.
headingStyles = {
color: "blue",
fontSize: "48px"
};
render() {
return (
<div>
<h3 style={this.headingStyles}>This is a heading</h3>
<p style={this.paragraphStyles}>This is a paragraph</p>
</div>
);
}
}
CSS Stylesheet: We can create a separate CSS file and write all the styles for the
component inside that file. This file needs to be imported inside the component file.
import './RandomComponent.css';
CSS Modules: We can create a separate CSS module and import this module inside our
component. Create a file with “.module.css”‘ extension, styles.module.css:
.paragraph{
color:"red";
border:1px solid black;
}
We can import this file inside the component and use it:
There are many ways through which one can optimize the performance of a React
app, let’s have a look at some of them:
Using useMemo( ) -
o It is a React hook that is used for caching CPU-Expensive functions.
o Sometimes in a React app, a CPU-Expensive function gets called repeatedly due to re-
renders of a component, which can lead to slow rendering.
useMemo( ) hook can be used to cache such functions. By using useMemo( ), the CPU-
Expensive function gets called only when it is needed.
Using React.PureComponent -
o It is a base component class that checks the state and props of a component to know
whether the component should be updated.
o Instead of using the simple React.Component, we can use React.PureComponent to
reduce the re-renders of a component unnecessarily.
Maintaining State Colocation -
o This is a process of moving the state as close to where you need it as possible.
o Sometimes in React app, we have a lot of unnecessary states inside the parent
component which makes the code less readable and harder to maintain. Not to forget,
having many states inside a single component leads to unnecessary re-renders for the
component.
o It is better to shift states which are less valuable to the parent component, to a separate
component.
Lazy Loading -
o It is a technique used to reduce the load time of a React app. Lazy loading helps reduce
the risk of web app performances to a minimum.
With the help of props, we can send data from a parent to a child component.
How do we do this?
return (
<div>
<button onClick={increment}>Increment Counter</button>
<ChildComponent counterValue={counter} />
</div>
);
}
As one can see in the code above, we are rendering the child component inside the
parent component, by providing a prop called counterValue. The value of the
counter is being passed from the parent to the child component.
We can use the data passed by the parent component in the following way:
function ChildComponent(props) {
return (
<div>
<p>Value of counter: {props.counterValue}</p>
</div>
);
}
Create a callback in the parent component which takes in the data needed as a parameter.
Pass this callback as a prop to the child component.
Send data from the child component using the callback.
We are considering the same example above but in this case, we are going to pass
the updated counterValue from child to parent.
Step1 and Step2: Create a callback in the parent component, pass this callback as
a prop.
function ParentComponent(props) {
let [counter, setCounter] = useState(0);
let callback = valueFromChild => setCounter(valueFromChild);
return (
<div>
<p>Value of counter: {counter}</p>
<ChildComponent callbackFunc={callback} counterValue={counter} />
</div>
);
}
As one can see in the code above, we created a function called callback which
takes in the data received from the child component as a parameter.
Next, we passed the function callback as a prop to the child component.
function ChildComponent(props) {
let childCounterValue = props.counterValue;
return (
<div>
<button onClick={() => props.callbackFunc(++childCounterValue)}>
Increment Counter
</button>
</div>
);
}
In the code above, we have used the props.counterValue and set it to a variable
called childCounterValue.
This way, we can pass data from the child to the parent component.
While developing React applications, we might develop components that are quite
similar to each other with minute differences. In most cases, developing similar
components might not be an issue but, while developing larger applications we
need to keep our code DRY, therefore, we want an abstraction that allows us to
define this logic in a single place and share it across components. HOC allows us
to create that abstraction.
Example of a HOC:
Notice the above components, both have similar functionality but, they are calling
different methods to an API endpoint.
In the code above, we have created a function called HOC which returns a
component and performs functionality that can be shared across both
the ArticlesList component and UsersList Component.
The second parameter in the HOC function is the function that calls the method on
the API endpoint.
There are four different phases in the lifecycle of React component. They are:
Initialization: During this phase, React component will prepare by setting up the default
props and initial state for the upcoming tough journey.
Mounting: Mounting refers to putting the elements into the browser DOM. Since React
uses VirtualDOM, the entire browser DOM which has been currently rendered would not
be refreshed. This phase includes the lifecycle
methods componentWillMount and componentDidMount.
Updating: In this phase, a component will be updated when there is a change in the state
or props of a component. This phase will have lifecycle methods
like componentWillUpdate, shouldComponentUpdate, render,
and componentDidUpdate.
Unmounting: In this last phase of the component lifecycle, the component will be
removed from the DOM or will be unmounted from the browser DOM. This phase will have
the lifecycle method named componentWillUnmount.
8. What are the lifecycle methods of React?
React lifecycle hooks will have the methods that will be automatically called at
different phases in the component lifecycle and thus it provides good control over
what happens at the invoked point. It provides the power to effectively control and
manipulate what goes on throughout the component lifecycle.
For example, if you are developing the YouTube application, then the application
will make use of a network for buffering the videos and it consumes the power of
the battery (assume only these two). After playing the video if the user switches to
any other application, then you should make sure that the resources like network
and battery are being used most efficiently. You can stop or pause the video
buffering which in turn stops the battery and network usage when the user switches
to another application after video play.
So we can say that the developer will be able to produce a quality application with
the help of lifecycle methods and it also helps developers to make sure to plan
what and how to do it at different points of birth, growth, or death of user interfaces.
constructor(): This method will be called when the component is initiated before
anything has been done. It helps to set up the initial state and initial values.
getDerivedStateFromProps() : This method will be called just before element(s)
rendering in the DOM. It helps to set up the state object depending on the initial props. The
getDerivedStateFromProps() method will have a state as an argument and it returns an
object that made changes to the state. This will be the first method to be called on an
updating of a component.
render(): This method will output or re-render the HTML to the DOM with new changes.
The render() method is an essential method and will be called always while the remaining
methods are optional and will be called only if they are defined.
componentDidMount(): This method will be called after the rendering of the component.
Using this method, you can run statements that need the component to be already kept in
the DOM.
shouldComponentUpdate() : The Boolean value will be returned by this method which will
specify whether React should proceed further with the rendering or not. The default value
for this method will be True.
getSnapshotBeforeUpdate() : This method will provide access for the props as well as
for the state before the update. It is possible to check the previously present value before
the update, even after the update.
componentDidUpdate(): This method will be called after the component has been
updated in the DOM.
componentWillUnmount() : This method will be called when the component removal from
the DOM is about to happen.
Static typing refers to the process of code check during the time of compilation for
ensuring all variables will be statically typed. React Hooks are functions that are
designed to make sure about all attributes must be statically typed. For enforcing
stricter static typing within our code, we can make use of the React API with
custom Hooks.
1. Built-in Hooks: The built-in Hooks are divided into 2 parts as given below:
Basic Hooks:
o useState(): This functional component is used to set and retrieve the state.
o useEffect(): It enables for performing the side effects in the functional components.
o useContext(): It is used for creating common data that is to be accessed by the
components hierarchy without having to pass the props down to each level.
Additional Hooks:
o useReducer() : It is used when there is a complex state logic that is having several sub-
values or when the upcoming state is dependent on the previous state. It will also enable
you to optimization of component performance that will trigger deeper updates as it is
permitted to pass the dispatch down instead of callbacks.
o useMemo() : This will be used for recomputing the memoized value when there is a change
in one of the dependencies. This optimization will help for avoiding expensive calculations
on each render.
o useCallback() : This is useful while passing callbacks into the optimized child
components and depends on the equality of reference for the prevention of unneeded
renders.
o useImperativeHandle(): It will enable modifying the instance that will be passed with
the ref object.
o useDebugValue(): It is used for displaying a label for custom hooks in React DevTools.
o useRef() : It will permit creating a reference to the DOM element directly within the
functional component.
o useLayoutEffect(): It is used for the reading layout from the DOM and re-rendering
synchronously.
It will not require a declaration of any kind of It is necessary to declare the constructor inside the
constructor. class component.
React Hooks can be helpful in implementing Because of the long setup of state declarations,
Redux and context API. class states are generally not preferred.
React Hooks will avoid a lot of overheads such as the instance creation, binding of events,
etc., that are present with classes.
Hooks in React will result in smaller component trees since they will be avoiding the
nesting that exists in HOCs (Higher Order Components) and will render props which result
in less amount of work to be done by React.
Our goal is for Hooks to cover all the functionalities for classes at its earliest. There
are no Hook equivalents for the following methods that are not introduced in Hooks
yet:
getSnapshotBeforeUpdate()
getDerivedStateFromError()
componentDidCatch()
Since it is an early time for Hooks, few third-party libraries may not be compatible
with Hooks at present, but they will be added soon.
React Router refers to the standard library used for routing in React. It permits us
for building a single-page web application in React with navigation without even
refreshing the page when the user navigates. It also allows to change the browser
URL and will keep the user interface in sync with the URL. React Router will make
use of the component structure for calling the components, using which appropriate
information can be shown. Since React is a component-based framework, it’s not
necessary to include and use this package. Any other compatible routing library
would also work with React.
BrowserRouter: It is a router implementation that will make use of the HTML5 history API
(pushState, popstate, and event replaceState) for keeping your UI to be in sync with the
URL. It is the parent component useful in storing all other components.
Routes: It is a newer component that has been introduced in the React v6 and an upgrade
of the component.
Route: It is considered to be a conditionally shown component and some UI will be
rendered by this whenever there is a match between its path and the current URL.
Link: It is useful in creating links to various routes and implementing navigation all over
the application. It works similarly to the anchor tag in HTML.
The React Hook cannot be considered as a replacement for Redux (It is an open-
source, JavaScript library useful in managing the application state) when it comes
to the management of the global application state tree in large complex
applications, even though the React will provide a useReducer hook that manages
state transitions similar to Redux. Redux is very useful at a lower level of
component hierarchy to handle the pieces of a state which are dependent on each
other, instead of a declaration of multiple useState hooks.
Conditional rendering refers to the dynamic output of user interface markups based
on a condition state. It works in the same way as JavaScript conditions. Using
conditional rendering, it is possible to toggle specific application functions, API data
rendering, hide or show elements, decide permission levels, authentication
handling, and so on.
Using if-else conditional logic which is suitable for smaller as well as for medium-sized
applications
Using ternary operators, which takes away some amount of complication from if-else
statements
Using element variables, which will enable us to write cleaner code.
I will assume that you are having some coding knowledge about JavaScript and
have installed Node on your system for creating a below given React Hook
program. An installation of Node comes along with the command-line tools: npm
and npx, where npm is useful to install the packages into a project and npx is
useful in running commands of Node from the command line. The npx looks in the
current project folder for checking whether a command has been installed there.
When the command is not available on your computer, the npx will look in the
npmjs.com repository, then the latest version of the command script will be loaded
and will run without locally installing it. This feature is useful in creating a skeleton
React application within a few key presses.
Open the Terminal inside the folder of your choice, and run the following command:
JSX will permit you for writing HTML-style template syntax directly into the
JavaScript file. This mixture of JavaScript and HTML will be converted by React
toolchain into pure JavaScript that will render the HTML element.
It is possible to define your own React components by writing a function that will
return a JSX element. You can try this by creating a new
file src/SearchItem.jsand put the following code into it.
Now, from the logo.svg, import will be removed and then contents of returned value
in the function App() will be replaced with the following code:
<div className="App">
<header>
Items with Hooks
</header>
<SearchItem/>
</div>
You can notice that the element <SearchItem/> has been used just similar to an
HTML element. The JSX syntax will enable for including the components in this
approach directly within the JavaScript code. Your application can be tested by
running the below-given command in your terminal.
npm start
This command will compile your application and open your default browser
into http://localhost:4000. This command can be kept on running when code
development is in progress to make sure that the application is up-to-date, and also
this browser page will be reloaded each time you modify and save the code.
This application will work finely, but it doesn’t look nice as it doesn’t react to any
input from the user. You can make it more interactive by adding a state with React
Hooks, adding authentication, etc.
A switching component refers to a component that will render one of the multiple
components. We should use an object for mapping prop values to components.
A below-given example will show you how to display different pages based on page
prop using switching component:
Using the below-given code, we can render the view when the browser is resized.
componentWillMount() {
this.updateDimension()
}
componentDidMount() {
window.addEventListener('resize', this.updateDimension)
}
componentWillUnmount() {
window.removeEventListener('resize', this.updateDimension)
}
updateDimension() {
this.setState({width: window.innerWidth, height: window.innerHeight})
}
render() {
return <span>{this.state.width} x {this.state.height}</span>
}
}
Passing data between sibling components of React is possible using React Router
with the help of history.push and match.params.
In the code given below, we have a Parent component AppDemo.js and have two
Child Components HomePage and AboutPage. Everything is kept inside a Router by
using React-router Route. It is also having a route for /about/{params} where we
will pass the data.
Also, the functional component AboutPage will obtain the data passed
by props.match.params.aboutId.
After button click in the HomePage the page will look like below:
21. How to perform automatic redirect after login?
The react-router package will provide the component <Redirect> in React Router.
Rendering of a <Redirect> component will navigate to a newer location. In the
history stack, the current location will be overridden by the new location just like the
server-side redirects.