React-Question Answer
React-Question Answer
React Router Dom is used for handling navigation and routing in React applications. It enables the
creation of single-page applications (SPAs) by allowing components to render based on the URL.
BrowserRouter is a type of router provided by React Router Dom. It uses the HTML5 history API to
keep UI in sync with the URL. It is typically used at the top level of the application to provide routing
functionality.
Routes is a component that wraps a set of Route components. Route is used to define a mapping
between a URL path and the component that should be rendered when the path is matched.
The path prop in Route specifies the pathname that the route matches. It can include parameters to
capture dynamic segments of the URL.
The element prop in Route is used to specify the React element to render when the route matches.
Link is a component provided by React Router Dom for creating links in the application. It prevents
the default behavior of a traditional anchor (<a>) tag and allows the application to navigate without a
full page reload.
useParams() is a hook in React Router Dom that returns an object of key/value pairs of URL
parameters. It is used to access the dynamic segments of the current URL.
Dynamic routing in React Router Dom involves defining dynamic segments in the route path using :
and then using useParams() to access the values of those segments.
If * is used as the path in a Route, it acts as a wildcard, matching any pathname. It is often used for
creating a fallback or 404 page.
useNavigate is a hook provided by React Router Dom for programmatically navigating between pages.
It returns a function that you can call with the desired location to navigate.
What is useEffect?
useEffect is a hook in React that allows performing side effects in functional components. It is used for
tasks like data fetching, subscriptions, manual DOM manipulations, and more.
A side effect in React refers to any action that is performed in a component that does not directly
relate to rendering. This includes tasks like data fetching, updating the DOM, or interacting with
external APIs.
How to make useEffect work only the first time the page renders?
You can make useEffect run only once by passing an empty dependency array ([]) as the second
argument to it.
You include the state variable that you want to watch in the dependency array. useEffect will then run
whenever that specific state changes.
Clean up in useEffect involves canceling subscriptions, clearing intervals, or performing any necessary
cleanup before the component is unmounted. This is done in the function returned by useEffect.
Omit the dependency array or include all the variables that the effect depends on in the dependency
array. This causes useEffect to run on every render.
Yes, you can add multiple useEffect hooks in a component, each handling different side effects or
subscriptions.
The Context API is a feature in React that allows the sharing of state between components without
explicitly passing props through every level of the component tree.
Props drilling occurs when you need to pass data through several layers of components by sending
props from one component to another, even if the intermediate components do not need the data.
You create a context using createContext() from the react module. For example: const MyContext =
React.createContext();
You provide a value to the context provider, and any component within the provider's subtree can
access that value using useContext hook.
What is the value prop in Context API?
The value prop in the context provider is used to set the value that will be shared with all the
components within the provider's subtree.
You can use the useContext hook to get the value from the context in a functional component.
Batch updates in React refer to the mechanism by which multiple state updates or renderings are
grouped together to optimize performance. React batches updates to minimize the number of
renders.
The previous state in useState is the state value before the most recent update. It can be obtained
using the callback form of setState.
What is useReducer?
useReducer is a hook in React that is used for managing complex state logic in a component. It takes a
reducer function and an initial state as arguments.
While both manage state, useReducer is preferable for complex state logic where the next state
depends on the previous one. useState is simpler and suitable for independent state updates.
dispatch is a function returned by useReducer that is used to trigger a state change. It takes an action
as an argument.
The reducer function in useReducer is responsible for specifying how the state should change in
response to different actions. It takes the current state and an action as parameters.
A React element is a lightweight representation of a DOM element. It is a plain JavaScript object that
describes what you want to see on the screen.
A React component is a reusable, self-contained piece of UI that can be composed together to build
complex user interfaces.
A component renders when its state or props change. React compares the previous and current
states/props and updates the component if there are differences