Skip to content

Latest commit

 

History

History
718 lines (429 loc) · 25.7 KB

File metadata and controls

718 lines (429 loc) · 25.7 KB

Q1. what is react?

=> React is a JavaScript library for building user interfaces. It was developed and is maintained by Meta (formerly Facebook) and has become one of the most popular tools for creating modern web applications.

Q2. what are key feactures of react?

=> 1.Component-Based Architecture:

  • React breaks the UI into reusable, independent pieces called components.

  • Components allow developers to build large applications by combining small, isolated pieces of UI.

2.Declarative:

  • You describe what the UI should look like, and React takes care of updating it when the data changes.

  • This makes code easier to read and debug.

3.Virtual DOM:

  • React uses a Virtual DOM (a lightweight copy of the real DOM) to efficiently manage UI updates.

  • It only updates the parts of the real DOM that need changes, leading to better performance.

4.Unidirectional Data Flow:

  • Data flows from parent to child components, which makes state management predictable and easier to debug.

5.JSX (JavaScript XML):

  • React allows developers to write HTML-like syntax directly in JavaScript using JSX.

  • Example: Hello, World! is a JSX element.

6.State and Props:

  • State: Used for managing dynamic data in a component.

  • Props: Short for "properties," used to pass data from a parent to a child component.

7.Rich Ecosystem:

  • React has a huge ecosystem of tools, libraries, and frameworks (like Next.js) for things like state management, routing, animations, and more.

Q3. Why Use React?

  • Reusable Code: Components can be reused across different parts of the app.

  • Performance: Virtual DOM optimizes updates.

  • Community: Large developer community and extensive documentation.

  • Cross-Platform Development: React is the basis for React Native, which allows building mobile apps for iOS and Android.

Q4. what are the limitations of react?

1. Learning Curve:

  • React introduces concepts like JSX, components, props, state, and hooks, which can be challenging for beginners.

  • Understanding additional tools like Redux, Context API, or routing libraries may increase complexity.

2. Incomplete Framework:

  • React is a library, not a full-fledged framework, so you need to rely on third-party tools for functionalities like routing (e.g., React Router) and state management (e.g., Redux, MobX, or Context API).

3. Poor SEO Out-of-the-Box:

  • React applications rely heavily on client-side rendering, which can negatively impact SEO if not optimized properly.

  • Server-side rendering (SSR) solutions, like Next.js, are required to address this issue.

4. Frequent Updates:

  • React evolves rapidly, introducing new APIs, features, and deprecating older ones, which can be overwhelming for developers to keep up with.

5. Boilerplate Code:

  • Managing state, handling lifecycle methods, and integrating third-party libraries can sometimes lead to verbose and complex code.

6. Performance Issues in Large Applications:

  • Improper state management or too many re-renders can cause performance bottlenecks in large-scale applications.

  • Optimization techniques like memoization or using React.memo are often necessary.

7. Steep Dependency on Third-Party Libraries:

  • React requires external libraries for common tasks like form handling (e.g., react-hook-form), HTTP requests (e.g., Axios), or animations (e.g., GSAP or Framer Motion).

  • This can lead to dependency bloat and compatibility issues.

8. Limited Opinionation:

  • React does not enforce a specific structure or approach, leaving developers with too many choices, which can lead to inconsistencies in large teams or projects.

9. Tooling Complexity:

  • Setting up a React project often involves tools like Webpack, Babel, and ESLint, which can be daunting for beginners.

10. Memory Consumption:

  • For simple applications, React's overhead (e.g., the virtual DOM) might be excessive, making it less suitable for smaller or simpler projects.

Q5. what is jsx?

  • JSX (JavaScript XML) is a syntax extension for JavaScript used in React. It allows you to write HTML-like code directly in JavaScript, making it easier to define the structure of the UI.

  • JSX is not HTML; it's transformed into JavaScript under the hood (e.g., React.createElement). It improves readability and allows embedding dynamic data and logic using {}.

Q6. What is XML?

  • XML stands for eXtensible Markup Language

  • XML is a markup language much like HTML

  • XML was designed to store and transport data

  • XML was designed to be self-descriptive

  • XML is a W3C Recommendation

Q7. why can't browsers read jsx

  • Browsers can't read JSX because it is not valid JavaScript or HTML—it’s a syntax extension specifically for React. JSX needs to be transpiled (converted) into plain JavaScript using tools like Babel before it can be understood and executed by the browser.

Q8. what is react components?

React components are the building blocks of a React application. They are reusable, independent pieces of UI that define what should appear on the screen. Components can be thought of as JavaScript functions or classes that return React elements (JSX).

Types of React Components:

  1. Functional Components:

    • Simple JavaScript functions that return JSX.

    Example:

    • function Greeting() { return <> Hello, World!</>}
  2. Class Components (less commonly used now):

    • ES6 classes that extend React.Component.

    Example:

    • class Greeting extends React.Component { render() { return Hello, World!; }}

Features of Components:

  • Reusable: Can be reused in different parts of the app.

  • Composable: Components can be nested within other components.

  • Isolated: Each component manages its own logic and state.

Q9. what is difference between functional component and class component?

Feature Functional Component Class Component
Definition Simple JavaScript function that returns JSX. ES6 class that extends React.Component.
State Management Uses Hooks (e.g., useState, useEffect). Manages state using this.state and setState().
Lifecycle Methods Handled with Hooks (e.g., useEffect). Uses lifecycle methods (e.g., componentDidMount).
Syntax cleaner and more concise. More verbose and requires render() method.
Performance Slightly faster (no overhead of classes). Slightly slower due to class overhead.
Usage Preferred in modern React development. Mostly legacy; less common now.

Q10. what is the differences between controlled and uncontrolled components?

Feature Controlled Component Uncontrolled Component
Definition The component's state is fully controlled by React. The component maintains its own internal state.
Data Handling Data is managed through React's state and updated via props. Data is accessed directly from the DOM (e.g., refs).
Control React has full control over the input's value. React does not control the input's value directly.
Value Access Value is accessed via state or onChange handlers. Value is accessed via refs (e.g., inputRef.current.value).
Example Use Case Forms with validation, dynamic updates, or stricter control. Simple forms where minimal React integration is needed.
Code Complexity Requires more code to handle state and events. Simpler code but less predictable behavior.

Q11. what is the differences between state and props in react?

| Feature | State | Props | |---------------------|=----------------------------------------------------------|----------------------------------------------------------| | Definition | A component's internal data that it manages itself. | Data passed to a component from its parent component. | | Mutability | Mutable: Can be updated with setState or Hooks like useState. | Immutable: Cannot be changed by the receiving component. | | Scope | Scoped to the component that owns and manages it. | Available to child components via props. | | Usage | Used for managing dynamic data that changes over time. | Used for passing data or methods between components. | | Updates | Can trigger re-renders when updated. | Cannot trigger re-renders directly (changes depend on the parent). | | Example Use Case| Form inputs, toggles, counters, etc. | Titles, configuration options, event handlers, etc. | | Access | Accessed with this.state (class) or useState (function). | Accessed with this.props (class) or function arguments. |

Q12. what is hooks in react , and name some common hooks in react?

Hooks are special functions in React that let you use state and lifecycle features in functional components, eliminating the need for class components. Introduced in React 16.8, hooks make it easier to manage component logic and reuse stateful logic across components.

Common React Hooks:

  1. Basic Hooks:

    • useState: Manages state in functional components.

    • useEffect: Handles side effects like data fetching, subscriptions, or DOM manipulations.

    • useContext: Accesses values from React's Context API.

  2. Additional Hooks:

    • useReducer: A more advanced way to manage state using reducers, similar to Redux.

    • useRef: Accesses or manipulates DOM elements and persists values across renders.

    • useMemo: Memoizes expensive calculations to improve performance.

    • useCallback: Memoizes callback functions to prevent unnecessary re-creations.

    • useLayoutEffect: Similar to useEffect, but fires synchronously after DOM updates.

  3. Custom Hooks:

    • Developers can create their own hooks to reuse logic (e.g., a custom hook for form handling or data fetching).

Key Benefits of Hooks:

  • Simplify code by using functional components.

  • Reuse stateful logic through custom hooks.

  • Eliminate complex patterns like higher-order components (HOCs) and render props.

Q13. how does usestate and useeffect work in react ?

How useState Works:

useState is a React Hook used to manage state in functional components. It lets you define a state variable and a function to update it.

Syntax:

  • state: The current state value.

  • setState: A function to update the state.

  • initialValue: The initial value of the state.

Example:

import React, { useState } from "react";

function Counter() {
  const [count, setCount] = useState(0); // Initialize state with 0

  return (
    <div>
      <p>Count: {count}</p>
      <button onClick={() => setCount(count + 1)}>Increment</button>
    </div>
  );
}
export default Counter;
  • When setCount is called, React re-renders the component with the updated state.

How useEffect Works:

useEffect is a React Hook used to handle side effects in functional components, such as data fetching, subscriptions, or DOM manipulation.

Syntax:

  • effectFunction: A function that contains the side effect logic.

  • dependencies: An array of variables that determine when the effect should re-run. If empty ([]), the effect runs only once after the initial render.

Example:

import React, { useState, useEffect } from "react";

export function Timer() {
  const [seconds, setSeconds] = useState(0);

  useEffect(() => {
    const interval = setInterval(() => {
      setSeconds((prev) => prev + 1);
    }, 1000);

    return () => clearInterval(interval); // Cleanup to avoid memory leaks
  }, []); // Run only once after the initial render

  return <p>Timer: {seconds} seconds</p>;
}

Key Points:

  1. useState:

    • Manages component-specific state.

    • Triggers a re-render when the state changes.

  2. useEffect:

    • Runs after render, handling side effects.

    • Dependencies ensure it runs only when necessary.

    • Can return a cleanup function to handle unmounting or cleanup logic.

Together, useState and useEffect allow functional components to handle dynamic behavior and side effects like class component

Q14. what is the importance of key in react?

The key prop in React is a special attribute used to uniquely identify elements in a list. It helps React efficiently update and render components when the list changes by identifying which items have been added, removed, or updated.

Importance of key in React:

  1. Efficient Reconciliation:

    • React uses keys during its reconciliation process to differentiate between items and determine which parts of the DOM need to be updated.

    • Without a unique key, React will re-render the entire list, which can hurt performance.

  2. Avoiding Bugs:

    • When keys are not unique or are missing, React may reuse DOM elements incorrectly, leading to unexpected UI behavior (e.g., incorrect state in reused components).
  3. Improved Performance:

    • Unique keys allow React to optimize rendering by only updating the affected elements instead of re-rendering the whole list.

Example of Key Usage:

Correct Use of key:

function TodoList({ todos }) {
  return (
    <ul>
      {todos.map((todo) => (
        <li key={todo.id}>{todo.text}</li> // Use a unique identifier
      ))}
    </ul>
  );
}   

Incorrect Use (Avoid using index as key):

function TodoList({ todos }) {
  return (
    <ul>
      {todos.map((todo, index) => (
        <li key={index}>{todo.text}</li> // Index is not unique and can cause bugs
      ))}
    </ul>
  );
}

Key Takeaways:

  • Keys should be unique for each element within a list.

  • Use a stable identifier (like id) instead of an array index to avoid potential issues.

  • Keys are critical for React's internal mechanisms but have no effect on the visual output.

Q15. explain the concept of lifting up the state in react?

Lifting Up State in React

Lifting up the state is a concept in React where the state is moved from a child component to a common parent component. This is done when multiple components need to share the same state, allowing them to communicate and synchronize data.

Why Lift State Up?

In React, each component manages its own state locally. However, when two or more child components need access to the same state or need to interact with each other, lifting the state up to their nearest common ancestor is an effective way to solve the issue.

By lifting state up, the parent component can manage the state and pass it down to the child components as props. This ensures that the state is shared across components, and updates to it are reflected in all components that depend on it.

How Does It Work?

  1. Move the state from the child components to the common parent.

  2. The parent component holds the state and provides functions to modify it.

  3. The parent passes the state and any functions to update it as props to the child compon

Example:

function Counter({ count, increment }) {
  return <button onClick={increment}>Increment: {count}</button>;
}

function Display({ count }) {
  return <h1>Current Count: {count}</h1>;
}

function App() {
  const [count, setCount] = useState(0);

  const increment = () => setCount(count + 1);

  return (
    <div>
      <Counter count={count} increment={increment} />
      <Display count={count} />
    </div>
  );
}

Key Takeaways:

  • Lifting state up makes it possible for child components to share and synchronize data.

  • The parent component manages the state and passes it down via props to the child components.

  • This pattern helps keep components declarative and ensures that the data flow is controlled.

By using lifting state up, React components can communicate efficiently without having to pass data deeply through multiple layers of components.

Q16. how does one pass data between components in react ?

In React, there are several ways to pass data between components, depending on the relationship between them. The most common methods are props, state lifting, and context. Here's an overview:

1. Passing Data via Props (Parent to Child)

Props (short for "properties") are the primary way to pass data from a parent component to a child component. Props are read-only, meaning that a child component cannot modify the props it receives.

Example:

function Parent() {
  const message = "Hello from Parent!";

  return <Child message={message} />;
}

function Child({ message }) {
  return <h1>{message}</h1>;
}

  • Parent passes the message as a prop to Child.

  • Child receives the prop as a parameter and uses it in its rendering.

2. Passing Data via State and Lifting State Up (Child to Parent)

When a child component needs to send data to a parent component (or multiple child components need to share state), you lift the state up to the common parent and pass a callback function from the parent to the child. The child calls this function to send data back to the parent.

Example:

function Parent() {
  const [message, setMessage] = useState("");

  const handleMessageChange = (newMessage) => {
    setMessage(newMessage);
  };

  return (
    <div>
      <Child onMessageChange={handleMessageChange} />
      <p>{message}</p>
    </div>
  );
}

function Child({ onMessageChange }) {
  return (
    <input
      type="text"
      onChange={(e) => onMessageChange(e.target.value)}
      placeholder="Type a message"
    />
  );
}

  • Parent holds the state message and passes handleMessageChange as a prop to Child.

  • Child updates the message state by calling the passed onMessageChange function.

3. Passing Data via Context (Across Multiple Levels)

For deeply nested components or when props need to be passed down multiple levels, you can use React Context. Context allows you to share values across the component tree without explicitly passing props through every level.

Example:

const MessageContext = React.createContext();

function Parent() {
  const [message, setMessage] = useState("Hello from Parent!");

  return (
    <MessageContext.Provider value={{ message, setMessage }}>
      <Child />
    </MessageContext.Provider>
  );
}

function Child() {
  return <GrandChild />;
}

function GrandChild() {
  const { message, setMessage } = useContext(MessageContext);

  return (
    <div>
      <p>{message}</p>
      <button onClick={() => setMessage("Updated by GrandChild!")}>Change Message</button>
    </div>
  );
}

  • MessageContext.Provider in the Parent provides the state and setter function.

  • useContext in GrandChild consumes the context value, allowing access to the state and the function to update it.

4. Passing Data via Ref (Accessing DOM or Child Component Directly)

While less common for passing regular data, refs can be used to directly access a child component or DOM element. This is typically used for focusing input elements, triggering animations, etc.

Example:

function Parent() {
  const inputRef = useRef();

  const focusInput = () => {
    inputRef.current.focus(); // Directly focus the input element
  };

  return (
    <div>
      <Child ref={inputRef} />
      <button onClick={focusInput}>Focus Input</button>
    </div>
  );
}

const Child = React.forwardRef((props, ref) => {
  return <input ref={ref} />;
});

  • forwardRef is used in the Child to pass the ref down from the Parent.

Key Points:

  • Props: Used to pass data from parent to child.

  • State Lifting: Used to send data from child to parent by moving the state to a common ancestor.

  • Context: Provides a way to share data between components at any level of the tree without passing props manually.

  • Ref: Provides a way to access child components or DOM elements directly.

Each method serves a different use case, and it's important to choose the right one based on the complexity of the data flow in your application.

Q17. what are the new feature introduce in react 18?

React 18 introduced several new features and improvements, with a focus on performance and concurrent rendering. Here are the most notable features:

1. Concurrent Rendering (Concurrent Mode)

  • Concurrent Rendering enables React to work on multiple tasks at once. It makes React apps feel more responsive by allowing React to interrupt rendering work to keep the UI responsive.

  • React can now suspend rendering and resume it later, enabling smoother transitions and updates, especially for large apps with heavy content.

  • To opt-in to concurrent rendering, use the createRoot API instead of the previous ReactDOM.render.

Example:

import ReactDOM from 'react-dom/client';
import App from './App';

const root = ReactDOM.createRoot(document.getElementById('root'));
root.render(<App />);

2. Automatic Batching

  • React 18 introduces automatic batching of updates, meaning multiple state updates (even from different event handlers or async operations) can be batched together to avoid unnecessary re-renders and improve performance.

  • This works across all React events, including setTimeout, Promises, and more.

Example:

import { useState } from 'react';

function Component() {
  const [state1, setState1] = useState(0);
  const [state2, setState2] = useState(0);

  const updateStates = () => {
    setState1(prev => prev + 1);
    setState2(prev => prev + 1);
  };

  return <button onClick={updateStates}>Update States</button>;
}
  • In React 18, both state updates will be batched together, improving rendering performance.

3. Suspense for Data Fetching

  • React 18 expands the Suspense API to work with data fetching. This allows you to suspend rendering while waiting for data (e.g., from an API) and show a loading state until the data is ready.

  • Concurrent rendering helps make the waiting process smoother and less blocking.

Example:

import { Suspense } from 'react';

function App() {
  return (
    <Suspense fallback={<div>Loading...</div>}>
      <DataFetchingComponent />
    </Suspense>
  );
}

4. useId Hook

  • The useId hook generates unique IDs that are stable across server and client renders, which helps with accessibility and ensures the IDs are consistent during hydration (in server-side rendering).

  • This is useful for managing form elements, modals, and components that need unique identifiers.

Example:

import { useId } from 'react';

function Form() {
  const id = useId();
  return <input id={id} />;
}

5. Concurrent Rendering Features in ReactDOM Server

  • React 18 introduced the ability to use concurrent rendering on the server side through Server-Side Rendering (SSR). This allows React to stream HTML to the client in chunks, improving performance for large apps.

  • Streaming SSR helps pages load faster by allowing browsers to start rendering parts of the page while other parts are still being generated on the server.

6. Transitions

  • Transitions allow React to differentiate between urgent and non-urgent updates, enabling more efficient rendering. Non-urgent updates can be delayed until the browser is idle, making UI interactions feel faster and smoother.

  • The startTransition API enables you to mark updates as "non-urgent", which can then be deferred.

Example:

import { startTransition } from 'react';

function handleChange(event) {
  startTransition(() => {
    setInput(event.target.value); // Non-urgent update
  });
}

7. ReactDOM.createRoot API

  • In React 18, ReactDOM's createRoot API is introduced as the primary way to initialize a React app. This API provides a more modern approach to rendering the app and automatically enables new features like concurrent rendering.

  • The new API should be used instead of ReactDOM.render.

8. Improved SSR (Server-Side Rendering) and Hydration

  • React 18 improves server-side rendering (SSR) and hydration by making the process faster and more efficient. It enables rendering of content incrementally from the server and hydration (attaching event handlers to HTML) in a more optimized manner.

  • This ensures that React apps are faster to load on the client side after being served from the server.

9. Streaming Server-Side Rendering (SSR) with Suspense

  • React 18 improves SSR by enabling streaming server-rendered HTML to the client in chunks.

  • This helps improve performance for large applications and decreases time-to-first-byte (TTFB).

10. useSyncExternalStore

  • This hook allows React to safely subscribe to external stores (like Redux or other state management libraries) and ensure that state is updated in sync with React’s rendering process.

  • It provides a reliable API for external state synchronization, which helps avoid issues like inconsistent UI or layout shifts.