React Points
React Points
Ans-1
1. JSX
JSX serves as a syntax extension to JavaScript, facilitating the
combination of HTML structures with JavaScript code within React
files.
2. Components
JSX serves as a syntax extension to JavaScript, facilitating the
combination of HTML structures with JavaScript code within
React files.
3. Virtual DOM
React employs a Virtual DOM, which is a lightweight
representation of the actual DOM stored in memory. This
approach allows React to selectively update only the relevant
parts of the real DOM when the state of an object changes.
4. Data Binding
React adopts a one-way data-binding approach, ensuring a
modular and efficient structure. Unidirectional data flow signifies
that in a React app, child components are often nested within
parent components.
5. High Performance
React's high performance is driven by its ability to update only
Q-2
What is difference between
Element
An Element is a simple object that describes what you want to show
on the screen. It defines the structure of DOM nodes or other
components. Elements can include other Elements in their properties.
Once created, Elements cannot be changed. Creating a React
Element is a straightforward and inexpensive operation.
JSX
type: 'div',
props: {
children: 'Login',
id: 'login-btn'
JSX
"login-btn" }, "Login");
Component
props as input and return a JSX tree as output. Components are more
Login
</div>
);
React.createElement(
"div",
"Login"
);
Ans-3
1. Function Components
Function components are the simplest way to create a component in
React. They are pure JavaScript functions that take a props object as
the first parameter and return React elements to display the output.
2. Class Components
Alternatively, you can use ES6 classes to define a component. The
equivalent class component for the above function component would
look like this:
JSX
render() {
return <h1>{`Greetings,
${this.props.userName}!`}</h1>;
Why is it Needed?
When we make changes to a webpage, like updating a list, traditional
methods often involve updating the entire webpage, even if only a
small part has changed. This can be slow and inefficient.
How Does it Work?
3. Faster Updates
Updating the virtual DOM is much faster than updating the real DOM.
It's like working on a draft before finalising a document.
see what's different. It then updates only the parts that have changed
that makes updating web pages faster and more efficient by smartly
figuring out what needs to change and updating only those parts.
Easy
Q-5
we need them?
Ans-5
Example
Suppose you have a list of books:
JSX
const books = [
];
You can use the "key" prop when mapping over this array
JSX
key={book.id}>{book.title}</li>);
Note
A If your data doesn't have stable IDs, using the item index as a key
Easy
Ans-6
64 Install Node
Open the terminal and run the following command to create a new
React application (replace my-react-app with your preferred
application name):
JSX
JSX
cd my-react-app
Print "Hello World!" Example
Now, open the src/App.js file and replace its content with the
following:
JSX
function App() {
return (
<div>
<h1>Hello World!</h1>
</div>
);
npm start
This will open your new React application in a web browser, and you
should see "Hello World!" displayed on the webpage.
Ans-7
Comments in React/JSX are similar to JavaScript multiline comments
but are enclosed in curly braces.
Single-line comments
JSX
<div>
</div>
Multi-line comments
JSX
<div>
{/*
*/}
</div>
Explain how
Q-8 in React? lists are created
Ans-8
Lists are essential for displaying dynamic content on a website. In
React, you can create a list using the map method of an array. Here's
an example:
JSX
import React from 'react';
});
ReactDOM.render(
<ul>
{fruitList}
</ul>,
document.getElementById('root')
);
In this modified example, we have a list of fruits, and the map method
is used to create a list of JSX elements (<li> elements) dynamically.
Each fruit is represented as a list item, and the resulting list is
rendered inside an unordered list (<ul>) in the specified HTML
element with the ID 'root'. The key attribute is added to each <li>
element for better performance and React's internal tracking of list
items.
Easy
Ans-9
Functional Components
1. Definition
A functional component is a plain JavaScript pure function that
accepts props as an argument.
2. Rendering
Does not use the render method. Instead, the component's
return value represents the UI.
3. State
Cannot use state. It is also known as a stateless component.
4. Lifecycle Methods
Cannot use React lifecycle methods (e.g., componentDidMount).
5. Constructor
Does not use a constructor.
Class Components
1. Definition
2. Rendering
3. State
4. Lifecycle Methods
5. Constructor
Summary
and return JSX. They are stateless and don't use a constructor
function Counter() {
return (
<div>
<p>Count: {count}</p>
</div>
);
Ans-11
function Greeting() {
return (
<div>
<p>{message}, React!</p>
</div>
);
Q-12
Hooks in React?
Ans-12
Basic Hooks
useState()
Used to manage and retrieve state in functional components.
useEffect()
Enables performing side effects in functional components, like
data fetching or DOM manipulation.
useContext()
Creates shared data accessible by components in a hierarchy
without passing props through each level.
Additional Hooks
useReducer()
Helpful for complex state logic or when the next state depends on
the previous state, optimizing performance by passing dispatch
down.
useMemo()
Avoids expensive calculations by recomputing memoized values
only when dependencies change.
useCallback()
useImperativeHandle()
useDebugValue()
useRef()
functional component.
useLayoutEffect()
rendering.
Custom Hooks
Custom Hooks
Ans-13
Usage
Wrap parts of the application in <React.StrictMode> to
activate additional checks and warnings.
Example
In the example below, strict mode checks apply to
<ComponentOne> and <ComponentTwo>.
JSX
function ExampleApplication() {
return (
<div>
<Header />
<React.StrictMode>
<div>
<ComponentOne />
<ComponentTwo />
</div>
</React.StrictMode>
<Header />
</div>
);
Ans-14
Medium
Q-15
the component lifecycle?
Ans-15
1. Initialization
In this phase, the React component gets ready by setting up
default props and initializing the state.
2. Mounting
Mounting involves putting the elements into the browser DOM.
React utilizes VirtualDOM, and during mounting, only the
changed elements are updated in the browser DOM. This phase
includes the following lifecycle methodss
¢ componentWillMoun
¢ componentDidMount
3. Updating
lifecycle methods
8 componentWillUpdat-
8 shouldComponentUpdat-
8 rende$
8 componentDidUpdate
4. Unmounting
method
8 componentWillUnmount
Easy
Q-16
React?
Ans-16
Example Scenario
Consider a YouTube application. When a user switches to another
app after playing a video, efficient resource management is
crucial. Lifecycle methods help developers ensure optimal
utilization of resources like network and battery.
1. constructor()
2. getDerivedStateFromProps()
4. componentDidMount()
DOM.
5. shouldComponentUpdate()
with rendering.
6. getSnapshotBeforeUpdate()
7. componentDidUpdate()
8. componentWillUnmount()
DOM.
Medium
Ans-17
The lifecycle of a React component is divided into four phases:
Example Scenario
R Consider a scenario where <EditUsersPage /> maintains
selectedUserAddress in its state.D
R <EditUsersPage /> renders <User />, which, in turn,
renders <UserDetails />.D
R <UserDetails /> contains a <UserAddress />
component that requires access to selectedUserAddress.
Approach
R The straightforward solution is to pass
selectedUserAddress as a prop from <EditUsersPage /
> to <User />, then to <UserDetails />, and finally to
<UserAddress />.
1. Alternative Approach
2. React Context
3. Benefits
components.
In React, components are a big deal, and React Router uses this
concept. You don't have to use React Router, but it's a popular
choice for managing navigation.
the path you set, this component decides what UI to show. It's
4. Link
application.
In simpler terms, React Router is like a guide for your React app,
reloading the entire page. It's a way to organize and manage how
Ans-19
Purpose
Custom Hooks provide a way to extract and manage complex
logic outside of components, promoting code reuse and
maintaining a clean and modular codebase.
Example
Consider a custom hook for handling form input:
JSX
// useInput.js
setValue(e.target.value);
};
return {
value,
onChange: handleChange,
};
};
Usage in a Component
J Now, you can use the useInput custom hook in any
component to manage input state:
JSX
return (
<form>
<label>Username:
</label>
<label>Password:
</label>
</form>
);
};
Explanation
& The useInput hook abstracts away the state management and
event handling for input fields.
& The component using this custom hook can easily manage
multiple input fields without duplicating similar logic.
in React?
Ans-20
Definition
> HOCs in React are functions that take a component and return
an enhanced version, leveraging React's compositional nature.
Purity of HOCs
> Often termed "pure components," HOCs accept any child
component without altering its behavior.
Usage Pattern
> Create an enhanced component using a higher-order function:
JSX
const EnhancedComponent =
higherOrderComponent(WrappedComponent);
Use Cases
> Code Reuse and Logic Abstraction
-> Encapsulate and reuse code, abstracting logic for enhanced
components.
> Render Hijacking
-> Customize component rendering by intercepting and
modifying the process.
- State and Props Manipulation
-> Manage state within HOCs, manipulate or enhance props
before passing them down.
Advantages
- Modularity and Separation of Concerns
-> Enhances code organization by separating concerns like
state, logic, and rendering.
- Composability
-> Compose multiple HOCs for granular and reusable
component composition.
- Encapsulation
-> Encapsulates specific functionalities, improving code clarity
and testability.