Skip to content

sarveshhome/react-practice-demo

Repository files navigation

ReactPracticeDemo

Install React Router using Yarn

yarn add react-router-dom

Run them together!

nx run-many -p react-practice-demo -t build test lint

nx graph

Generate UI lib

nx g @nx/react:lib ui

Add a component

nx g @nx/react:component ui/src/lib/button

React Interview Question

  • Controlled Components

    • In a controlled component, the form data is handled by the component's state. The state is updated using the onChange event handler, and the component's value is set using the value attribute.
  • Uncontrolled Components

    • In an uncontrolled component, the form data is handled by the DOM itself. You can access the value using a ref.
Feature Controlled Components Uncontrolled Components
Data Handling Managed by React state Managed by the DOM
Accessing Values Through state variables Using refs (useRef)
Validation Can be done on each input change Typically done on form submission
Default Values Set via React state Set using defaultValue attribute
Use Case Complex forms, real-time validation Simple forms, quick prototyping

keys in React can we use index as key

🔁 Example: Bad vs Good Keys ❌ Bad (with index):

{items.map((item, index) => (
  <li key={index}>{item.name}</li>
))}

✅ Good (with stable ID):

{items.map((item) => (
  <li key={item.id}>{item.name}</li>
))}

✅ How to Handle Async Data Fetching in React

  • Handling asynchronous data fetching in React involves managing side effects, loading state, error state, and rendering the fetched data.

  • Key Concepts State Purpose loading Shows loader/spinner while data is fetched error Displays error messages gracefully users Holds fetched data

  • ✅ Best Practices

    • Use try/catch/finally: For robust error and loading handling.

    • Avoid state updates after unmount:

      • Use AbortController or a mounted flag.
    • Show loading or fallback UI: So users know something is happening.

Using Error Boundary in Function Component

Step 1: npm install react-error-boundary

Step 2: Implement in a Functional Component

Wrap Your Component

Summary Answer for Interview

  • "In React, error boundaries are typically implemented using class components, but in function components I use the react-error-boundary library, which gives me the same capabilities. I apply error boundaries around parts of the UI that may throw errors during rendering—like dashboards, third-party widgets, or routes. It helps prevent the whole app from crashing and allows for graceful fallback UIs and error logging."

Error boundaries occur by ErrorFallBack component

Generic form

yarn add react-hook-form

✨ Your new, shiny Nx workspace is almost ready ✨.

Learn more about this workspace setup and its capabilities or run npx nx graph to visually explore what was created. Now, let's get you up to speed!

Finish your CI setup

Click here to finish setting up your workspace!

Run tasks

To run the dev server for your app, use:

npx nx serve react-practice-demo

To create a production bundle:

npx nx build react-practice-demo

To see all available targets to run for a project, run:

npx nx show project react-practice-demo

These targets are either inferred automatically or defined in the project.json or package.json files.

More about running tasks in the docs »

Add new projects

While you could add new projects to your workspace manually, you might want to leverage Nx plugins and their code generation feature.

Use the plugin's generator to create new projects.

To generate a new application, use:

npx nx g @nx/react:app demo

To generate a new library, use:

npx nx g @nx/react:lib mylib

You can use npx nx list to get a list of installed plugins. Then, run npx nx list <plugin-name> to learn about more specific capabilities of a particular plugin. Alternatively, install Nx Console to browse plugins and generators in your IDE.

Learn more about Nx plugins » | Browse the plugin registry »

Learn more about Nx on CI

Install Nx Console

Nx Console is an editor extension that enriches your developer experience. It lets you run tasks, generate code, and improves code autocompletion in your IDE. It is available for VSCode and IntelliJ.

Install Nx Console »

Useful links

Learn more:

And join the Nx community: