React Key Points
React Key Points
What is ReactJS?
State
● State is a local data storage for a component that can change over
time.
● It is mutable, meaning it can be updated using functions like
useState in functional components or setState in class
components.
● State is used for managing dynamic data, such as form inputs,
button clicks, or API responses.
●
Props
● Props (short for properties) are used to pass data from a parent
component to a child component.
● They are immutable, meaning the child cannot modify them.
● Props enable communication between components and are often
used to make components reusable.
How it works:
Uncontrolled Component:
What are the loaders and plugins in webpack? Can you explain what
they are and how they differ?
Loaders and plugins are both essential parts of webpack, but they serve
different purposes. Loaders are used to preprocess files before they are
added to the bundle. For example, you might use a loader like Babel to
transpile your ES6 JavaScript code into ES5, which is compatible with
older browsers. Plugins, on the other hand, are used to perform a wider
range of tasks, such as bundle optimization, asset management, and
environment configuration. They hook into the webpack compilation
process and can manipulate the bundle in various ways.
𝐀𝐧 𝐞𝐱𝐚𝐦𝐩𝐥𝐞 𝐟𝐨𝐫 𝐭𝐡𝐢𝐬 𝐰𝐨𝐮𝐥𝐝 𝐛𝐞 𝐝𝐞𝐜𝐨𝐫𝐚𝐭𝐢𝐧𝐠 𝐚 𝐜𝐚𝐤𝐞. 𝐋𝐨𝐚𝐝𝐞𝐫𝐬 𝐚𝐫𝐞 𝐥𝐢𝐤𝐞 𝐩𝐫𝐞𝐩𝐚𝐫𝐢𝐧𝐠
𝐭𝐡𝐞 𝐢𝐧𝐠𝐫𝐞𝐝𝐢𝐞𝐧𝐭𝐬 – 𝐦𝐢𝐱𝐢𝐧𝐠 𝐭𝐡𝐞 𝐛𝐚𝐭𝐭𝐞𝐫, 𝐚𝐝𝐝𝐢𝐧𝐠 𝐟𝐥𝐚𝐯𝐨𝐫𝐬, 𝐞𝐭𝐜. – 𝐰𝐡𝐢𝐥𝐞 𝐩𝐥𝐮𝐠𝐢𝐧𝐬 𝐚𝐫𝐞
𝐥𝐢𝐤𝐞 𝐚𝐝𝐝𝐢𝐧𝐠 𝐟𝐫𝐨𝐬𝐭𝐢𝐧𝐠, 𝐝𝐞𝐜𝐨𝐫𝐚𝐭𝐢𝐨𝐧𝐬, 𝐚𝐧𝐝 𝐟𝐢𝐧𝐚𝐥 𝐭𝐨𝐮𝐜𝐡𝐞𝐬 𝐭𝐨 𝐦𝐚𝐤𝐞 𝐭𝐡𝐞 𝐜𝐚𝐤𝐞 𝐥𝐨𝐨𝐤
𝐚𝐧𝐝 𝐭𝐚𝐬𝐭𝐞 𝐞𝐯𝐞𝐧 𝐛𝐞𝐭𝐭𝐞𝐫.
What is tree shaking, and how does webpack utilise it to optimise
bundles?
Tree shaking is a process used to remove unused code from our bundle.
It works by analysing the code and identifying which modules and
exports are not being used anywhere in your application. Then, it
eliminates those unused parts from the final bundle, resulting in a
smaller and more efficient package.
Imagine you have multiple components that need to fetch data from an
API. Instead of duplicating the data-fetching logic in each component,
you can create an HOC to handle it.
Basic Use - Consider a component that renders a list of items and has
performance concerns when the list grows large or the parent
component frequently re-renders. Using PureComponent can help
ensure that this component only re-renders when its actual data
changes.
What is the use of the react-dom package?
The react-dom package provides DOM-specific methods that can be
used at the top level of our app. Most of the components are not
required to use this module. Some of the methods of this package are:
i. render()
ii. hydrate()
iii.unmountComponentAtNode()
iv.findDOMNode()
v. createPortal()
2. useEffect
4. useReducer
5. useCallback
6. useMemo
7. useLayoutEffect
8. useTransition
9.useRef
2. Context API
1. State Update Logic: Reducers contain the logic to determine how
the state changes based on the type of action dispatched.
2. Pure Function: Reducers must always produce the same output for
the same input, without causing side effects (e.g., API calls or
mutations).
3. Immutable Updates: Reducers create new state objects without
modifying the existing state, ensuring immutability
React and Next.js are both powerful tools for building web
applications, but they serve slightly different purposes and
have their own advantages and disadvantages. Here's a
breakdown:
React
Pros:
Cons:
Next.js
Pros:
1.Server-Side Rendering (SSR): Built-in support for SSR
improves SEO and performance.
2.Static Site Generation (SSG): Allows for pre-rendering
pages at build time, which can enhance loading speed.
3.File-Based Routing: Simplifies routing with a
straightforward file structure.
4.API Routes: Enables building serverless functions directly
within the application.
5.Automatic Code Splitting: Optimizes loading by only
loading the necessary code for each page.
Cons:
Which to Prefer?
2. Frequent Re-renders
useEffect VS useLayoutEffect
useEffect
Description:
Usage:
Cons:
useLayoutEffect
Description:
Usage:
Pros:
Cons:
When to Use
● Use useEffect:
○ For side effects that don't require immediate DOM
updates.
○ When dealing with APIs, subscriptions, or timers.
● Use useLayoutEffect:
○ When you need to measure or modify the DOM
immediately after changes.
○ For animations or layout calculations that require
immediate access to the DOM before painting.
Summary
Lifecycle Diagram
● Mounting: constructor() →
getDerivedStateFromProps() → render() →
componentDidMount()
● Updating: getDerivedStateFromProps() →
shouldComponentUpdate() → render() →
getSnapshotBeforeUpdate() → componentDidUpdate()
● Unmounting: componentWillUnmount()