Skip to the content.

React Coding Practice

Q. Create a multilevel dropdown menu in React?

Input: [
  {
    label: "Menu 1",
  },
  {
    label: "Menu 2",
    submenu: [{ label: "Sub Menu 1" }, { label: "Sub Menu 2" }],
  },
  {
    label: "Menu 3",react-coding-practice.md
    submenu: [
      { label: "Sub Menu 1" },
      { label: "Sub Menu 2" },
      { label: "Sub Menu 3" },
      { label: "Sub Menu 4" },
    ],
  },
  {
    label: "Menu 4",
    submenu: [{ label: "Sub Menu 1" }, { label: "Sub Menu 2" }],
  },
];
Answer ```js import React, { useState } from "react"; const menuData = [ { label: "Menu 1" }, { label: "Menu 2", submenu: [{ label: "Sub Menu 1" }, { label: "Sub Menu 2" }], }, { label: "Menu 3", submenu: [ { label: "Sub Menu 1" }, { label: "Sub Menu 2", submenu: [ { label: "Nested Sub Menu 1" }, { label: "Nested Sub Menu 2" }, ], }, { label: "Sub Menu 3" }, ], }, { label: "Menu 4", submenu: [{ label: "Sub Menu 1" }, { label: "Sub Menu 2" }], }, ]; function MenuItem({ item }) { const [open, setOpen] = useState(false); const hasSubmenu = Array.isArray(item.submenu) && item.submenu.length > 0; return ( <li style=> <button onClick={() => hasSubmenu && setOpen((prev) => !prev)} style= > {item.label} {hasSubmenu ? (open ? "-" : "+") : ""} </button> {hasSubmenu && open && ( <ul style= > {item.submenu.map((subItem, index) => ( <MenuItem key={`${subItem.label}-${index}`} item={subItem} /> ))} </ul> )} </li> ); } export default function App() { return ( <div style=>

Multi-Level Dropdown Menu

<ul style=> {menuData.map((item, index) => ( <MenuItem key={`${item.label}-${index}`} item={item} /> ))} </ul> </div> ); } ```
↥ back to top

Q. Create a functional component that displays data from https://reqres.in/api/users?

Answer ```js import { useEffect, useState } from "react"; import axios from "axios"; export default function App() { const [users, setUsers] = useState<{ id: number; name: string; email: string }[]>([]); useEffect(() => { axios.get("https://jsonplaceholder.typicode.com/users").then((response) => { setUsers(response.data); }); }, []); return (
    {users.map((user) => ( <li key={user.id}> {user.name} - {user.email} </li> ))}
); } ``` **⚝ [Try this example on CodeSandbox](https://codesandbox.io/s/react-rest-api-hmcx8p?file=/src/App.js)**
↥ back to top

Q. Write a program to display searched keyword in React?

Answer ```js import { useState } from "react"; function App() { const [search, setSearch] = useState(""); return (

Update Data from an input

Searched Keyword: {search}
<input className="input" type="text" value={search} placeholder="Search Here" onChange={(e) => setSearch(e.target.value)} />
); } export default App; ``` **⚝ [Try this example on CodeSandbox](https://codesandbox.io/s/react-update-dom-vu4il?file=/src/index.js)**
↥ back to top

Q. Create a functional component to show an alert message based on user input?

Answer ```js import { useEffect, useState } from "react"; function App() { const [phrase, setPhrase] = useState(""); useEffect(() => { if (phrase === "Hello React") { alert("You may pass!"); } }, [phrase]); return (

What is the secret phrase?

<input type="text" value={phrase} onChange={(e) => setPhrase(e.target.value)} placeholder="Enter a secret" />

Hint: It is Hello React

); } export default App; ``` **⚝ [Try this example on CodeSandbox](https://codesandbox.io/s/react-alert-based-on-input-hk2ip?file=/src/index.js)**
↥ back to top

Q. Create a functional component in react to add two numbers?

Answer ```js import { useState } from "react"; export default function App() { const [number1, setNumber1] = useState(0); const [number2, setNumber2] = useState(0); return (

Adding Two Numbers

<input type="number" value={number1} onChange={(e) => setNumber1(+e.target.value)} /> <input type="number" value={number2} onChange={(e) => setNumber2(+e.target.value)} />

Total: {number1 + number2}

); } ``` **⚝ [Try this example on CodeSandbox](https://codesandbox.io/s/react-calculator-8ud1d?file=/src/index.js)**
↥ back to top

Q. Create a simple counter in react?

Answer ```js import { useCallback, useState } from "react"; const App = () => { const [counter, setCounter] = useState(0); const increment = useCallback(() => setCounter((c) => c + 1), []); const decrement = useCallback(() => setCounter((c) => c - 1), []); return (

Counter: {counter}

<button onClick={increment}>Increment</button> <button onClick={decrement}>Decrement</button>
); }; export default App; ``` **⚝ [Try this example on CodeSandbox](https://codesandbox.io/s/react-counter-bhp4q?file=/src/App.js)**
↥ back to top

Q. Write a program to pass values to child using context in React?

Answer ```js // Counter.js const { useState, useContext } = React; const CountContext = React.createContext(); const Counter = () => { const { count, increase, decrease } = useContext(CountContext); return (

<button onClick={decrease}>Decrement</button> {count} <button onClick={increase}>Increment</button>

); }; ``` ```js // App.js const App = () => { const [count, setCount] = useState(0); const increase = () => { setCount(count + 1); }; const decrease = () => { setCount(count - 1); }; return (
); }; ``` **⚝ [Try this example on CodeSandbox](https://codesandbox.io/s/react-context-api-v8syu?file=/src/index.js)**
↥ back to top

Q. Create a simple ToDo list app using React?

Answer ```js import { useCallback, useState } from "react"; interface Todo { id: number; text: string; completed: boolean; } export default function App() { const [todos, setTodos] = useState<Todo[]>([]); const [input, setInput] = useState(""); const addTodo = useCallback(() => { const trimmed = input.trim(); if (!trimmed) return; setTodos((prev) => [ ...prev, { id: Date.now(), text: trimmed, completed: false }, ]); setInput(""); }, [input]); const toggleTodo = useCallback((id: number) => { setTodos((prev) => prev.map((todo) => todo.id === id ? { ...todo, completed: !todo.completed } : todo, ), ); }, []); const deleteTodo = useCallback((id: number) => { setTodos((prev) => prev.filter((todo) => todo.id !== id)); }, []); const handleKeyDown = useCallback( (e: React.KeyboardEvent) => { if (e.key === "Enter") addTodo(); }, [addTodo], ); return (

ToDo List

<input type="text" value={input} onChange={(e) => setInput(e.target.value)} onKeyDown={handleKeyDown} placeholder="Add a new task…" /> <button onClick={addTodo}>Add</button>
{todos.length === 0 ? (

No tasks yet. Add one above!

) : (
    {todos.map((todo) => ( <li key={todo.id}> <input type="checkbox" checked={todo.completed} onChange={() => toggleTodo(todo.id)} /> <span style= > {todo.text} </span> <button onClick={() => deleteTodo(todo.id)} aria-label="Delete task" > X </button> </li> ))}
)} {todos.length > 0 && (

{todos.filter((t) => t.completed).length} / {todos.length} completed

)}
); } ``` **⚝ [Try this example on CodeSandbox](https://codesandbox.io/s/react-todo-list-hw45y?file=/src/App.js)** </details>
↥ back to top
## Q. Create a search filter component in react? ```js Input: const people = [ "Shashi Koshy", "Dhriti Taneja", "Dipa Mishra", "Ansh Thakkar", "Lakshmi Thaker", "Sushila Matthai", "Shresth Nigam", "Bhavana Biswas", "Vasudha Mangat", "Priya Saran" ]; ```
Answer ```js import { useMemo, useState } from "react"; export default function App() { const [searchItem, setSearchItem] = useState(""); const searchResults = useMemo( () => people.filter((person) => person.toLowerCase().includes(searchItem.toLowerCase()), ), [searchItem], ); return (
<input type="text" placeholder="Search" value={searchItem} onChange={(e) => setSearchItem(e.target.value)} />
    {searchResults.map((item) => ( <li key={item}>{item}</li> ))}
); } ``` **⚝ [Try this example on CodeSandbox](https://codesandbox.io/s/react-search-list-u1s8b?file=/src/index.js)**
↥ back to top
## Q. Create a Fizz Buzz program in React? ```js Counting incrementally, replacing any number divisible by three with the word "fizz", and any number divisible by five with the word "buzz". ```
Answer ```js import { useState } from "react"; function fizzBuzz(n: number): string | number { if (n % 15 === 0) return "FizzBuzz"; if (n % 3 === 0) return "Fizz"; if (n % 5 === 0) return "Buzz"; return n; } export default function FizzBuzz() { const [count, setCount] = useState(1); return (

React Fizz Buzz Program

Counting incrementally, replacing any number divisible by three with the word “fizz”, and any number divisible by five with the word “buzz”.

{fizzBuzz(count)}

<button onClick={() => setCount((c) => Math.max(1, c - 1))}>-</button> <button onClick={() => setCount((c) => c + 1)}>+</button>
); } ``` **⚝ [Try this example on CodeSandbox](https://codesandbox.io/s/react-fizz-buzz-qtk36?file=/src/index.js)**
↥ back to top
## Q. Write a program to call child method from parent in React?
Answer **1. Using React.forwardRef():** ```js import { forwardRef, useRef, useImperativeHandle } from "react"; const Child = forwardRef((props, ref) => { useImperativeHandle(ref, () => ({ getMessage() { alert("Message from Child"); }, })); return

Child Component

; }); const Parent = () => { const childRef = useRef(); return (
<Child ref={childRef} /> <button onClick={() => childRef.current.getMessage()}>Click</button>
); }; ``` **⚝ [Try this example on CodeSandbox](https://codesandbox.io/s/react-forwardref-3serh?file=/src/index.js)** **2. Using Class Component:** ```js class Parent extends React.Component { constructor(props) { super(props); this.child = React.createRef(); } onClick = () => { this.child.current.getMessage(); }; render() { return (
<Child ref={this.child} /> <button onClick={this.onClick}>Click</button>
); } } class Child extends React.Component { getMessage() { alert("Message from Child"); } render() { return

Child Component

; } } ``` **⚝ [Try this example on CodeSandbox](https://codesandbox.io/s/react-createref-t0gud?file=/src/index.js)** **3. Using callback Ref API:** ```js class Parent extends React.Component { render() { return (
<Child ref={(instance) => { this.child = instance; }} /> <button onClick={() => { this.child.getMessage(); }} > Click </button>
); } } class Child extends React.Component { getMessage() { alert("Message from Child"); } render() { return

Child Component

; } } ``` **⚝ [Try this example on CodeSandbox](https://codesandbox.io/s/react-callback-ref-api-kp30y?file=/src/index.js)**
↥ back to top
## Q. Write a program to show and hide element in React?
Answer ```js import { useState } from "react"; export default function App() { const [show, setShow] = useState(true); return (
<button onClick={() => setShow((prev) => !prev)}> Toggle: {show ? "Hide" : "Show"} </button> {show &&

Hello World!

}
); } ``` **⚝ [Try this example on CodeSandbox](https://codesandbox.io/s/react-toggle-gipub?file=/src/App.js)**
↥ back to top
## Q. How to update the parent state in React?
Answer **Using function as a Prop:** ```js import { useState } from "react"; function Child({ text, onUpdate }: Readonly<{ text: string; onUpdate: (value: string) => void }>) { return ( <button onClick={() => onUpdate("Parent State Changed")}>{text}</button> ); } export default function Parent() { const [text, setText] = useState("Click Me!"); return <Child text={text} onUpdate={setText} />; } ``` **⚝ [Try this example on CodeSandbox](https://codesandbox.io/s/react-function-as-a-prop-2unfh?file=/src/App.js)**
↥ back to top
## Q. How do I reference a local image in React?
Answer ```js import React, { Component } from "react"; import logo from "./react_photo-goose.jpg"; export default class Header extends Component { render() { return (
<img src={logo} width="100%" alt="Googe Pic" />
); } } ``` **⚝ [Try this example on CodeSandbox](https://codesandbox.io/s/image-in-react-ud0ry?file=/src/App.js)**
↥ back to top
## Q. How to access a child state in React?
Answer Accessing child state via ref is an anti-pattern — state should be lifted to the parent. When you genuinely need to call a method on a child (e.g. focus an input, trigger an animation), use **useImperativeHandle** and **forwardRef**: ```js import { forwardRef, useImperativeHandle, useRef, useState } from "react"; const Child = forwardRef(function Child(_, ref) { const [value, setValue] = useState(""); useImperativeHandle(ref, () => ({ getValue: () => value, })); return <input value={value} onChange={(e) => setValue(e.target.value)} />; }); export default function Parent() { const childRef = useRef < { getValue: () => string } > null; return ( <> <Child ref={childRef} /> <button onClick={() => alert(childRef.current?.getValue())}>Read</button> </> ); } ``` **⚝ [Try this example on CodeSandbox](https://codesandbox.io/s/access-child-state-p2iip?file=/src/App.js)**
↥ back to top
## Q. How to access custom attributes from event object in React?
Answer **event.target.getAttribute:** ```js export default function Header() { const handleClick = (event: React.MouseEvent) => { console.log(event.currentTarget.getAttribute("name")); }; return ( <button name="Header" onClick={handleClick}> CLICK ME! </button> ); } ``` **⚝ [Try this example on CodeSandbox](https://codesandbox.io/s/react-custom-attributes-i3mu0?file=/src/index.js)** </details> ## Q. How to update state on props change in React?
Answer ```js import { useState } from "react"; function Counter({ onIncrement, count, }: Readonly<{ onIncrement: () => void; count: number }>) { return (
<button onClick={onIncrement}>CLICK ME</button>

{count}

); } export default function App() { const [count, setCount] = useState(1); return <Counter count={count} onIncrement={() => setCount((c) => c + 1)} />; } ``` **⚝ [Try this example on CodeSandbox](https://codesandbox.io/s/react-change-state-by-props-phjde?file=/src/index.js)**
## Q. Write a program to delete an item from array in React?
Answer ```js import { useState } from "react"; type User = { id: number; name: string }; const initialUsers: User[] = [ { id: 101, name: "Tanu Kanda" }, { id: 102, name: "Sathwik Bhatti" }, { id: 103, name: "Vritika Nath" }, { id: 104, name: "Chanda Mittal" }, { id: 105, name: "Sumati Pau" }, ]; export default function ListComponent() { const [users, setUsers] = useState<User[]>(initialUsers); const deleteById = (id: number) => setUsers((prev) => prev.filter((user) => user.id !== id)); return (

Delete an item from state array

    {users.map((user) => ( <li key={user.id}> <button onClick={() => deleteById(user.id)}>X - </button> {user.name} </li> ))}
); } ``` **⚝ [Try this example on CodeSandbox](https://codesandbox.io/s/react-delete-an-item-3d9t2?file=/src/index.js)**
## Q. How to pass bearer token with axios **Authorization token in axios:** ```js import axios from "axios"; import { useEffect, useState } from "react"; const API_URL = "https://jsonplaceholder.typicode.com/users"; export default function Page() { const [data, setData] = useState(null); const [error, setError] = useState<string | null>(null); useEffect(() => { const fetchData = async () => { const raw = sessionStorage.getItem("data"); if (!raw) throw new Error("No session data found."); const token: string = JSON.parse(raw).data.id; const res = await axios.get(API_URL, { headers: { Authorization: `Bearer ${token}` }, // Authorization token in axios }); setData(res.data); }; fetchData().catch((err: Error) => setError(err.message)); }, []); if (error) return

Error: {error}

; if (!data) return

Loading…

; return
{JSON.stringify(data, null, 2)}
; } ``` ## Q. Pass props in Link react-router **render props:** ```js import { BrowserRouter as Router, Link, Route, Routes } from "react-router-dom"; function IndexPage() { return

Home Page

; } function PropsPage({ title }: Readonly<{ title: string }>) { return

{title}

; } export default function App() { return (
<Route path="/" element={} /> <Route path="/props-through-component" element={} /> <Route path="/props-through-render" element={} />
); } ``` **⚝ [Try this example on CodeSandbox](https://codesandbox.io/s/pass-props-in-react-router-xs34i?file=/src/index.js)** ## Q. How to disable a button when an input is empty? ```js import { useState } from "react"; export default function App() { const [email, setEmail] = useState(""); return (
<input type="email" placeholder="Email" value={email} onChange={(e) => setEmail(e.target.value)} /> <button disabled={email.length < 1}>Submit</button>
); } ``` **⚝ [Try this example on CodeSandbox](https://codesandbox.io/s/react-disable-a-button-yb8gm?file=/src/App.js)** ## Q. Update style of a component onScroll in React.js ```js import { useState, useRef, useCallback } from "react"; const ITEMS = Array.from( { length: 50 }, () => "Get the scroll position on scroll in react.", ); const containerStyle = { height: 300, overflowY: "scroll" } as const; export default function App() { const prevScrollY = useRef(0); const [goingUp, setGoingUp] = useState(false); const onScroll = useCallback( (e: React.UIEvent) => { const currentScrollY = (e.target as HTMLDivElement).scrollTop; if (prevScrollY.current < currentScrollY && goingUp) { setGoingUp(false); } else if (prevScrollY.current > currentScrollY && !goingUp) { setGoingUp(true); } prevScrollY.current = currentScrollY; }, [goingUp], ); return ( <div onScroll={onScroll} style={containerStyle}> {ITEMS.map((text, i) => ( <p key={i}>{text}</p> ))} </div> ); } ``` **⚝ [Try this example on CodeSandbox](https://codesandbox.io/s/react-onscroll-event-dst5o?file=/src/App.js)** ## Q. How to generate unique IDs for form labels in React? **useId Hooks:** ```js import { useId } from "react"; export default function App() { const id = useId(); return (
<input id={id} type="checkbox" /> <label htmlFor={id}>label</label>
); } ``` **⚝ [Try this example on CodeSandbox](https://codesandbox.io/s/generate-unique-id-for-form-e7iqz?file=/src/App.js)** ## Q. How can one tell the version of React running at runtime in the browser? **version Hook** ```js import { version } from "react"; export default function App() { return (

React version: {version}

); } ``` **⚝ [Try this example on CodeSandbox](https://codesandbox.io/s/react-version-hpuy8?file=/src/App.js)** ## Q. Update React component every second ```js import { useState, useEffect } from "react"; export default function TimeComponent() { const [time, setTime] = useState(String(new Date())); useEffect(() => { const interval = setInterval(() => setTime(String(new Date())), 1000); return () => clearInterval(interval); }, []); return

Current Time: {time}

; } ``` **⚝ [Try this example on CodeSandbox](https://codesandbox.io/s/update-component-every-second-i0gk5?file=/src/App.js)** ## Q. How to declare a global variable in React? **Window Object:** ```js const name = "Hello React"; export default function App() { return (

Global variable using window object

{name}

); } ``` **⚝ [Try this example on CodeSandbox](https://codesandbox.io/s/global-variable-using-window-property-862oe?file=/src/App.js)** ## Q. Instance vs state variables in React ```js import { useRef, useState } from "react"; // Instance variable (useRef): changing it does NOT trigger a re-render. // State variable (useState): changing it DOES trigger a re-render. export default function Test() { const instanceValue = useRef(0); const [stateValue, setStateValue] = useState(0); function handleIncrementRef() { instanceValue.current += 1; alert( `Ref value is now ${instanceValue.current} (UI still shows old state)`, ); } return (

State variable: {stateValue}

<button onClick={handleIncrementRef}>Increment Ref (no re-render)</button> <button onClick={() => setStateValue((v) => v + 1)}> Increment State (re-renders) </button>
); } ``` **⚝ [Try this example on CodeSandbox](https://codesandbox.io/s/class-variable-vs-state-variable-gy9bj?file=/src/App.js)** ## Q. How to create dynamic href in react render function? ```js const posts = [ { id: 10, title: "Link One" }, { id: 20, title: "Link Two" }, { id: 30, title: "Link Three" }, ]; export default function App() { return (
    {posts.map((post) => ( <li key={post.id}> <a href={`/posts/${post.id}`}>{post.title}</a> </li> ))}
); } ``` **⚝ [Try this example on CodeSandbox](https://codesandbox.io/s/dynamic-href-in-react-63ewq?file=/src/App.js)** ## Q. How to toggle boolean state of react component? ```js import { useState } from "react"; export default function App() { const [good, setGood] = useState(true); return (
<button onClick={() => setGood((v) => !v)}>

Do you feel good today?

<span role="img" aria-label={good ? "Thumbs Up" : "Thumbs Down"}> {good ? "Yes! 👍" : "No! 👎"} </span>
</button>
); } ``` **⚝ [Try this example on CodeSandbox](https://codesandbox.io/s/toggle-boolean-state-in-react-kguly?file=/src/index.js)** ## Q. Dynamically add child components in React ```js // Parent.js export default class Parent extends React.Component { render() { return ( <>

Parent Component!

{this.props.children} </> ); } } ``` ```js // Child.js export default class Child extends React.Component { render() { return ( <>

Child Component!

</> ); } } ``` ```js // index.js import Parent from "./Parent"; import Child from "./Child"; const rootElement = document.getElementById("root"); ReactDOM.render( , rootElement, ); ``` **⚝ [Try this example on CodeSandbox](https://codesandbox.io/s/dynamically-add-child-components-nryzl?file=/src/index.js)** ## Q. Disable back button in react navigation _ToDo_ **⚝ [Try this example on CodeSandbox](https://codesandbox.io/s/react-disable-back-button-1651v?file=/src/index.js)** ## Q. How to restrict access to routes in react-router? ```js import { useState } from "react"; // useAuth: manages authentication state. Returns current auth status and login/logout actions. function useAuth() { const [isAuthenticated, setIsAuthenticated] = useState(false); return { isAuthenticated, login: () => setIsAuthenticated(true), logout: () => setIsAuthenticated(false) }; } // LoginPage: shown when the user is not authenticated. Calls onLogin when the user submits. function LoginPage({ onLogin }: Readonly<{ onLogin: () => void }>) { return (

Login

<button onClick={onLogin}>Log in</button>
); } // Dashboard: protected page shown only to authenticated users. Calls onLogout to sign out. function Dashboard({ onLogout }: Readonly<{ onLogout: () => void }>) { return (

Dashboard (protected)

<button onClick={onLogout}>Log out</button>
); } // App: renders LoginPage or Dashboard based on authentication state. export default function App() { const { isAuthenticated, login, logout } = useAuth(); if (!isAuthenticated) { return <LoginPage onLogin={login} />; } return <Dashboard onLogout={logout} />; } ``` ## Q. How do I set multipart in axios with react? ```js import { useRef } from "react"; import axios from "axios"; const UPLOAD_URL = "FILE_DIRECTORY"; async function uploadImage(file: File) { const formData = new FormData(); formData.append("filename", file); formData.append("destination", "images"); formData.append("create_thumbnail", "true"); await axios.post(UPLOAD_URL, formData, { headers: { "content-type": "multipart/form-data" }, }); } export default function App() { const fileRef = useRef<File | null>(null); async function handleUpload() { if (!fileRef.current) return; try { await uploadImage(fileRef.current); } catch (error) { console.error("Upload failed:", error); } } return (

File Upload in React

<input type="file" name="file" onChange={(e) => { fileRef.current = e.target.files?.[0] ?? null; }} /> <button onClick={handleUpload}>Upload</button>
); } ``` **⚝ [Try this example on CodeSandbox](https://codesandbox.io/s/file-upload-in-react-ubjei?file=/src/index.js)** ## Q. How to start search only when user stops typing? ```js import { useState, useEffect } from "react"; const DEBOUNCE_MS = 300; export default function App() { const [value, setValue] = useState(""); useEffect(() => { const id = setTimeout(() => { console.log(`Search called: "${value}"`); }, DEBOUNCE_MS); return () => clearTimeout(id); }, [value]); return ( <> <input value={value} onChange={(e) => setValue(e.target.value)} placeholder="Search" />

{value}

</> ); } ``` **⚝ [Try this example on CodeSandbox](https://codesandbox.io/s/react-search-using-settimeout-9d8vd?file=/src/App.js)** ## Q. How to implement default or NotFound page? ```js import { Link, BrowserRouter as Router, Route, Routes } from "react-router-dom"; const IndexPage = () =>

Home Page

; const AboutPage = () =>

About Page

; const NoMatchPage = () =>

Page Not Found

; export default function App() { return (
Home</Link> | About</Link> |{" "} 404</Link> <Route path="/" element={} /> <Route path="/about" element={} /> <Route path="*" element={} />
); } ``` **⚝ [Try this example on CodeSandbox](https://codesandbox.io/s/react-page-not-found-route-lnn2y?file=/src/index.js)** ## Q. How to focus an input element on page load? **autoFocus:** ```js export default function App() { return ( <> </> ); } ``` **⚝ [Try this example on CodeSandbox](https://codesandbox.io/s/focus-an-input-element-on-page-load-z1dx6?file=/src/App.js)** ## Q. Give a simple example of Jest test case? ```js // Counter.js import { useState } from "react"; export default function Counter() { const [count, setCount] = useState(0); return ( <>

{count}

<button onClick={() => setCount((c) => c + 1)}>Increment</button> <button onClick={() => setCount((c) => c - 1)}>Decrement</button> <button onClick={() => setCount(0)}>Reset</button> </> ); } ``` ```js // Counter.test.js import { render, screen, fireEvent } from "@testing-library/react"; import Counter from "./page"; describe("Counter", () => { it("renders with initial count of 0", () => { render(); expect(screen.getByTestId("count")).toHaveTextContent("0"); }); it("increments count when Increment is clicked", () => { render(); fireEvent.click(screen.getByText("Increment")); expect(screen.getByTestId("count")).toHaveTextContent("1"); }); it("decrements count when Decrement is clicked", () => { render(); fireEvent.click(screen.getByText("Increment")); fireEvent.click(screen.getByText("Decrement")); expect(screen.getByTestId("count")).toHaveTextContent("0"); }); it("resets count to 0 when Reset is clicked", () => { render(); fireEvent.click(screen.getByText("Increment")); fireEvent.click(screen.getByText("Increment")); fireEvent.click(screen.getByText("Reset")); expect(screen.getByTestId("count")).toHaveTextContent("0"); }); }); ``` **⚝ [Try this example on CodeSandbox](https://codesandbox.io/s/react-jest-test-case-5jsf2)** ## Q. How to use font-awesome icons in React? ```js import { FontAwesomeIcon } from "@fortawesome/react-fontawesome"; import { faAddressBook, faHome } from "@fortawesome/free-solid-svg-icons"; export default function App() { return (

React Font Awesome Icons

); } ``` **⚝ [Try this example on CodeSandbox](https://codesandbox.io/s/react-font-awesome-x2oq0?file=/src/index.js)** ## Q. How to use SVGs in React? ```js function App() { return (

SVG in React

<img src={"http://s.cdpn.io/3/kiwi.svg"} alt="Kiwi Bird" width="200px" height="200px" />
); } ``` **⚝ [Try this example on CodeSandbox](https://codesandbox.io/s/svg-in-react-d1mq4?file=/src/App.js)** ## Q. How to repeat an element n times using JSX? ```js export default function App() { return ( <>

Repeat an element n times using JSX

{Array.from({ length: 5 }, (_, i) => ( <div key={i}> Field {i}:
))} </div> </> ); } ``` **⚝ [Try this example on CodeSandbox](https://codesandbox.io/s/repeat-an-element-n-times-using-jsx-ze1yh?file=/src/App.js)** ## Q. How can I set a cookie in react? ```js import { CookiesProvider } from "react-cookie"; import { createRoot } from "react-dom/client"; import App from "./App"; const rootElement = document.getElementById("root"); createRoot(rootElement).render( , ); ``` ```js import { useState } from "react"; import { useCookies } from "react-cookie"; const App = () => { const [name, setName] = useState(""); const [cookies, setCookie] = useCookies(["name"]); const handle = () => { setCookie("name", name, { path: "/" }); }; return ( <>

Cookies in React

<input placeholder="Cookie value" value={name} onChange={(e) => setName(e.target.value)} /> <button onClick={handle}>Set Cookie</button> {cookies.name &&
Cookies Value: {cookies.name}
} </> ); }; export default App; ``` **⚝ [Try this example on CodeSandbox](https://codesandbox.io/s/repeat-an-element-n-times-using-jsx-ze1yh?file=/src/App.js)** ## Q. How to Create dependent dropdowns that populates data with each other in React.js? ```js /** * Dependent Dropdown in React */ import { useState } from "react"; const data = [ { name: "Delhi", cities: ["Siri", "Sultanpur", "Tughlqabad", "Jahanpanah", "Firozobad"] }, { name: "Maharashtra", cities: ["Mumbai", "Pune", "Nagpur", "Thane", "Nashik", "Jalgaon"] }, { name: "West Bengal", cities: ["Kolkata", "Asansol", "Siliguri", "Durgapur", "Baharampur"] }, { name: "Tamil Nadu", cities: ["Chennai", "Coimbatore", "Madurai", "Tiruchirappalli"] }, ]; export default function App() { const [capital, setCapital] = useState(""); const [cities, setCities] = useState<string[]>([]); const [city, setCity] = useState(""); function updateSelect(e: React.ChangeEvent) { const selected = e.target.value; setCapital(selected); setCities(data.find((d) => d.name === selected)?.cities ?? []); setCity(""); } return ( <> <select value={capital} onChange={updateSelect}> {data.map((d) => ( <option key={d.name} value={d.name}>{d.name}</option> ))} </select> <select value={city} onChange={(e) => setCity(e.target.value)}> {cities.map((city) => ( <option key={city} value={city}>{city}</option> ))} </select> </> ); } ``` **⚝ [Try this example on CodeSandbox](https://codesandbox.io/s/react-dependent-dropdowns-qs3e6o?file=/src/App.js)** #### Q. Create a slide show app ```js import { useState, useEffect, useCallback } from "react"; type Slide = { id: number; title: string; description: string; bg: string; }; const slides: Slide[] = [ { id: 1, title: "Mountains", description: "Majestic peaks touching the sky.", bg: "bg-blue-500" }, { id: 2, title: "Ocean", description: "Endless waves of calm and wonder.", bg: "bg-cyan-500" }, { id: 3, title: "Forest", description: "A canopy of green silence.", bg: "bg-green-600" }, { id: 4, title: "Desert", description: "Golden dunes shaped by the wind.", bg: "bg-yellow-500" }, ]; const AUTOPLAY_DELAY = 4000; export default function App() { const [current, setCurrent] = useState(0); const next = useCallback(() => setCurrent((i) => (i + 1) % slides.length), []); useEffect(() => { const timer = setInterval(next, AUTOPLAY_DELAY); return () => clearInterval(timer); }, [next]); return (
<section aria-label="Slideshow" className={`relative w-full max-w-2xl rounded-2xl overflow-hidden shadow-xl ${slides[current].bg} text-white transition-colors duration-500`} >

{slides[current].title}

{slides[current].description}

{slides.map((slide, i) => ( <button key={slide.id} onClick={() => setCurrent(i)} aria-label={`Go to slide ${i + 1}`} className={`w-2.5 h-2.5 rounded-full transition-colors ${ i === current ? "bg-white" : "bg-white/40" }`} /> ))}
</section>

{current + 1} / {slides.length}

); } ``` ## Q. Create a custom useFetch hook in React?
Answer ```js import { useEffect, useReducer } from "react"; type State = { data: T | null; loading: boolean; error: string | null }; type Action = | { type: "loading" } | { type: "success"; payload: T } | { type: "error"; payload: string }; function fetchReducer(state: State, action: Action): State { switch (action.type) { case "loading": return { data: null, loading: true, error: null }; case "success": return { data: action.payload, loading: false, error: null }; case "error": return { data: null, loading: false, error: action.payload }; } } function useFetch(url: string) { const [state, dispatch] = useReducer(fetchReducer, { data: null, loading: true, error: null, }); useEffect(() => { let cancelled = false; dispatch({ type: "loading" }); fetch(url) .then((res) => { if (!res.ok) throw new Error(`HTTP error: ${res.status}`); return res.json() as Promise; }) .then((data) => { if (!cancelled) dispatch({ type: "success", payload: data }); }) .catch((err: Error) => { if (!cancelled) dispatch({ type: "error", payload: err.message }); }); return () => { cancelled = true; }; }, [url]); return state; } // Usage type User = { id: number; name: string; email: string }; export default function App() { const { data, loading, error } = useFetch<User[]>( "https://jsonplaceholder.typicode.com/users" ); if (loading) return

Loading…

; if (error) return

Error: {error}

; return (
    {data?.map((user) => ( <li key={user.id}>{user.name} — {user.email}</li> ))}
); } ``` </details> ## Q. Create a Pagination component in React?
Answer ```js import { useMemo, useState } from "react"; const TOTAL_ITEMS = 100; const PAGE_SIZE = 10; function usePagination(total: number, pageSize: number) { const [page, setPage] = useState(1); const totalPages = Math.ceil(total / pageSize); const items = useMemo(() => { const start = (page - 1) * pageSize + 1; const end = Math.min(page * pageSize, total); return Array.from({ length: end - start + 1 }, (_, i) => start + i); }, [page, pageSize, total]); return { page, totalPages, items, setPage }; } export default function App() { const { page, totalPages, items, setPage } = usePagination(TOTAL_ITEMS, PAGE_SIZE); return ( <>

Pagination

    {items.map((n) => <li key={n}>Item {n}</li>)}
<div style=> <button disabled={page === 1} onClick={() => setPage((p) => p - 1)}>Prev</button> {Array.from({ length: totalPages }, (_, i) => i + 1).map((p) => ( <button key={p} onClick={() => setPage(p)} style= > {p} </button> ))} <button disabled={page === totalPages} onClick={() => setPage((p) => p + 1)}>Next</button> </div>

Page {page} of {totalPages}

</> ); } ```
## Q. Create a Star Rating component in React?
Answer ```js import { useState } from "react"; type StarRatingProps = Readonly<{ maxStars?: number; onChange?: (rating: number) => void; }>; function StarRating({ maxStars = 5, onChange }: StarRatingProps) { const [rating, setRating] = useState(0); const [hover, setHover] = useState(0); const handleSelect = (value: number) => { setRating(value); onChange?.(value); }; return ( <div style=> {Array.from({ length: maxStars }, (_, i) => i + 1).map((star) => ( <button key={star} aria-label={`${star} star${star > 1 ? "s" : ""}`} onClick={() => handleSelect(star)} onMouseEnter={() => setHover(star)} onMouseLeave={() => setHover(0)} style= > ★ </button> ))} <span style=> {rating > 0 ? `${rating} / ${maxStars}` : "No rating"} </span> </div> ); } export default function App() { return ( <div style=>

Rate this product

<StarRating onChange={(r) => console.log("Rating:", r)} /> </div> ); } ```
## Q. How to persist state to localStorage using a custom hook?
Answer ```js import { useState, useEffect } from "react"; function useLocalStorage(key: string, initialValue: T) { const [storedValue, setStoredValue] = useState(() => { try { const item = window.localStorage.getItem(key); return item ? (JSON.parse(item) as T) : initialValue; } catch { return initialValue; } }); useEffect(() => { try { window.localStorage.setItem(key, JSON.stringify(storedValue)); } catch { console.error("Failed to save to localStorage"); } }, [key, storedValue]); return [storedValue, setStoredValue] as const; } // Usage export default function App() { const [name, setName] = useLocalStorage("username", ""); return (

Persisted Input

<input value={name} onChange={(e) => setName(e.target.value)} placeholder="Your name (saved to localStorage)" />

Hello, {name || "stranger"}!

); } ``` </details> ## Q. How to implement infinite scroll in React?
Answer ```js import { useState, useEffect, useRef, useCallback } from "react"; const PAGE_SIZE = 20; function generateItems(page: number) { return Array.from({ length: PAGE_SIZE }, (_, i) => ({ id: (page - 1) * PAGE_SIZE + i + 1, text: `Item ${(page - 1) * PAGE_SIZE + i + 1}`, })); } export default function App() { const [items, setItems] = useState(generateItems(1)); const [page, setPage] = useState(1); const [loading, setLoading] = useState(false); const loaderRef = useRef(null); const loadMore = useCallback(() => { setLoading(true); setTimeout(() => { setPage((p) => { const next = p + 1; setItems((prev) => [...prev, ...generateItems(next)]); return next; }); setLoading(false); }, 500); }, []); useEffect(() => { const observer = new IntersectionObserver( (entries) => { if (entries[0].isIntersecting && !loading) loadMore(); }, { threshold: 1 } ); const el = loaderRef.current; if (el) observer.observe(el); return () => { if (el) observer.unobserve(el); }; }, [loadMore, loading]); return ( <div style=>

Infinite Scroll (page {page})

    {items.map((item) => <li key={item.id}>{item.text}</li>)}
<div ref={loaderRef} style=> {loading ? "Loading…" : "Scroll for more"} </div> </div> ); } ``` </details> ## Q. How to use useTransition for non-blocking UI updates in React?
Answer ```js import { useState, useTransition, useMemo } from "react"; const ALL_ITEMS = Array.from({ length: 10_000 }, (_, i) => `Item ${i + 1}`); export default function App() { const [query, setQuery] = useState(""); const [filterQuery, setFilterQuery] = useState(""); const [isPending, startTransition] = useTransition(); const filtered = useMemo( () => ALL_ITEMS.filter((item) => item.toLowerCase().includes(filterQuery.toLowerCase())), [filterQuery] ); const handleChange = (e: React.ChangeEvent) => { setQuery(e.target.value); // urgent — update input immediately startTransition(() => { setFilterQuery(e.target.value); // non-urgent — defer expensive filter }); }; return (

useTransition Demo

<input value={query} onChange={handleChange} placeholder="Filter 10,000 items…" /> {isPending && <p style=>Updating list…</p>}
    {filtered.slice(0, 100).map((item) => <li key={item}>{item}</li>)}
); } ``` **Key point:** `startTransition` marks the state update as non-urgent so React can interrupt it to keep the input responsive. </details> ## Q. How to create a form with validation in React?
Answer ```js import { useState } from "react"; type FormValues = { name: string; email: string; password: string }; type FormErrors = Partial<Record<keyof FormValues, string>>; function validate(values: FormValues): FormErrors { const errors: FormErrors = {}; if (!values.name.trim()) errors.name = "Name is required."; if (!values.email.includes("@")) errors.email = "Enter a valid email."; if (values.password.length < 8) errors.password = "Password must be at least 8 characters."; return errors; } export default function SignupForm() { const [values, setValues] = useState({ name: "", email: "", password: "" }); const [errors, setErrors] = useState({}); const [submitted, setSubmitted] = useState(false); const handleChange = (e: React.ChangeEvent) => { const { name, value } = e.target; setValues((prev) => ({ ...prev, [name]: value })); setErrors((prev) => ({ ...prev, [name]: undefined })); }; const handleSubmit = (e: React.FormEvent) => { e.preventDefault(); const errs = validate(values); if (Object.keys(errs).length > 0) { setErrors(errs); return; } setSubmitted(true); }; if (submitted) return

Form submitted successfully!

; return ( <form onSubmit={handleSubmit} noValidate style=>

Sign Up

{(["name", "email", "password"] as const).map((field) => ( <div key={field}> {errors[field] && <p style=>{errors[field]}</p>} </div> ))} </form> ); } ``` </details> ## Q. How to implement drag and drop list in React?
Answer ```js import { useState, useRef, type DragEvent } from "react"; const initialItems = ["Task A", "Task B", "Task C", "Task D", "Task E"]; export default function DragDropList() { const [items, setItems] = useState(initialItems); const dragIndex = useRef<number | null>(null); const handleDragStart = (index: number) => { dragIndex.current = index; }; const handleDrop = (dropIndex: number) => { if (dragIndex.current === null || dragIndex.current === dropIndex) return; const updated = [...items]; const [dragged] = updated.splice(dragIndex.current, 1); updated.splice(dropIndex, 0, dragged); setItems(updated); dragIndex.current = null; }; return ( <div style=>

Drag & Drop List

<ul style=> {items.map((item, index) => ( <li key={item} draggable onDragStart={() => handleDragStart(index)} onDragOver={(e: DragEvent) => e.preventDefault()} onDrop={() => handleDrop(index)} style= > ☰ {item} </li> ))} </ul> </div> ); } ```
## Q. Build an Autocomplete / Typeahead component in React?
Answer ```js import { useState, useRef, useEffect, useCallback } from "react"; const SUGGESTIONS = [ "Apple", "Banana", "Blueberry", "Cherry", "Coconut", "Grape", "Guava", "Kiwi", "Lemon", "Mango", "Orange", "Papaya", "Peach", "Pear", "Pineapple", "Plum", "Pomegranate", "Raspberry", "Strawberry", "Watermelon", ]; export default function Autocomplete() { const [query, setQuery] = useState(""); const [open, setOpen] = useState(false); const [activeIndex, setActiveIndex] = useState(-1); const listRef = useRef(null); const filtered = SUGGESTIONS.filter((s) => s.toLowerCase().startsWith(query.toLowerCase()) ); const select = useCallback((value: string) => { setQuery(value); setOpen(false); setActiveIndex(-1); }, []); const handleKeyDown = (e: React.KeyboardEvent) => { if (!open) return; if (e.key === "ArrowDown") setActiveIndex((i) => Math.min(i + 1, filtered.length - 1)); else if (e.key === "ArrowUp") setActiveIndex((i) => Math.max(i - 1, 0)); else if (e.key === "Enter" && activeIndex >= 0) select(filtered[activeIndex]); else if (e.key === "Escape") setOpen(false); }; useEffect(() => { if (listRef.current && activeIndex >= 0) { (listRef.current.children[activeIndex] as HTMLElement)?.scrollIntoView({ block: "nearest" }); } }, [activeIndex]); return ( <div style=>

Autocomplete

<input value={query} onChange={(e) => { setQuery(e.target.value); setOpen(true); setActiveIndex(-1); }} onKeyDown={handleKeyDown} onBlur={() => setTimeout(() => setOpen(false), 150)} onFocus={() => query && setOpen(true)} placeholder="Type a fruit…" style= aria-autocomplete="list" aria-expanded={open} /> {open && filtered.length > 0 && ( <ul ref={listRef} role="listbox" style= > {filtered.map((item, i) => ( <li key={item} role="option" aria-selected={i === activeIndex} onMouseDown={() => select(item)} style= > {item} </li> ))} </ul> )} </div> ); } ``` </details> ## Q. Build a Multi-step Form (Wizard) in React? *ToDo* ## Q. Build a Shopping Cart using useContext and useReducer in React?
Answer ```js import { createContext, useContext, useReducer, ReactNode } from "react"; type Product = { id: number; name: string; price: number }; type CartItem = Product & { quantity: number }; type CartAction = | { type: "ADD"; product: Product } | { type: "REMOVE"; id: number } | { type: "INCREMENT"; id: number } | { type: "DECREMENT"; id: number }; function cartReducer(state: CartItem[], action: CartAction): CartItem[] { switch (action.type) { case "ADD": { const existing = state.find((i) => i.id === action.product.id); if (existing) return state.map((i) => i.id === action.product.id ? { ...i, quantity: i.quantity + 1 } : i); return [...state, { ...action.product, quantity: 1 }]; } case "REMOVE": return state.filter((i) => i.id !== action.id); case "INCREMENT": return state.map((i) => i.id === action.id ? { ...i, quantity: i.quantity + 1 } : i); case "DECREMENT": return state.map((i) => i.id === action.id ? { ...i, quantity: Math.max(1, i.quantity - 1) } : i); default: return state; } } const CartContext = createContext<{ cart: CartItem[]; dispatch: React.Dispatch } | null>(null); function CartProvider({ children }: { children: ReactNode }) { const [cart, dispatch] = useReducer(cartReducer, []); return <CartContext.Provider value=>{children}</CartContext.Provider>; } function useCart() { const ctx = useContext(CartContext); if (!ctx) throw new Error("useCart must be inside CartProvider"); return ctx; } const PRODUCTS: Product[] = [ { id: 1, name: "React T-Shirt", price: 29 }, { id: 2, name: "Next.js Mug", price: 15 }, { id: 3, name: "TypeScript Hoodie", price: 55 }, ]; function ProductList() { const { dispatch } = useCart(); return (

Products

{PRODUCTS.map((p) => ( <div key={p.id} style=> {p.name} — ${p.price} <button onClick={() => dispatch({ type: "ADD", product: p })}>Add to Cart</button>
))} </div> ); } function Cart() { const { cart, dispatch } = useCart(); const total = cart.reduce((sum, i) => sum + i.price * i.quantity, 0); return (

Cart ({cart.reduce((s, i) => s + i.quantity, 0)} items)

{cart.length === 0 ?

Empty cart

: cart.map((item) => ( <div key={item.id} style=> <span style=>{item.name}</span> <button onClick={() => dispatch({ type: "DECREMENT", id: item.id })}>-</button> {item.quantity} <button onClick={() => dispatch({ type: "INCREMENT", id: item.id })}>+</button> <button onClick={() => dispatch({ type: "REMOVE", id: item.id })}>✕</button>
))} {cart.length > 0 && Total: ${total}} </div> ); } export default function App() { return (

Shopping Cart


); } ``` </details> ## Q. Create a reusable Modal / Dialog component in React?
Answer ```js import { useEffect, useRef, ReactNode } from "react"; import { createPortal } from "react-dom"; type ModalProps = Readonly<{ isOpen: boolean; onClose: () => void; title: string; children: ReactNode; }>; function Modal({ isOpen, onClose, title, children }: ModalProps) { const dialogRef = useRef(null); // Close on Escape key useEffect(() => { if (!isOpen) return; const handleKey = (e: KeyboardEvent) => { if (e.key === "Escape") onClose(); }; document.addEventListener("keydown", handleKey); return () => document.removeEventListener("keydown", handleKey); }, [isOpen, onClose]); // Trap focus inside modal useEffect(() => { if (isOpen) dialogRef.current?.focus(); }, [isOpen]); if (!isOpen) return null; return createPortal( <div role="presentation" onClick={onClose} style= > <div ref={dialogRef} role="dialog" aria-modal="true" aria-labelledby="modal-title" tabIndex={-1} onClick={(e) => e.stopPropagation()} style= > <div style=> <h2 id="modal-title" style=>{title}</h2> <button onClick={onClose} aria-label="Close modal" style=>✕</button> </div> {children} </div> </div>, document.body ); } // Usage import { useState } from "react"; export default function App() { const [open, setOpen] = useState(false); return ( <div style=> <button onClick={() => setOpen(true)}>Open Modal</button> <Modal isOpen={open} onClose={() => setOpen(false)} title="Confirm Action">

Are you sure you want to proceed?

<div style=> <button onClick={() => setOpen(false)}>Cancel</button> <button onClick={() => { alert("Confirmed!"); setOpen(false); }} style=>Confirm</button> </div> </Modal> </div> ); } ``` </details> ## Q. Create a reusable Tabs component in React?
Answer ```js import { useState, ReactNode } from "react"; type Tab = { label: string; content: ReactNode }; type TabsProps = Readonly<{ tabs: Tab[]; defaultIndex?: number }>; function Tabs({ tabs, defaultIndex = 0 }: TabsProps) { const [active, setActive] = useState(defaultIndex); return (
<div role="tablist" style=> {tabs.map((tab, i) => ( <button key={tab.label} role="tab" aria-selected={i === active} aria-controls={`panel-${i}`} id={`tab-${i}`} onClick={() => setActive(i)} style= > {tab.label} </button> ))}
{tabs.map((tab, i) => ( <div key={tab.label} role="tabpanel" id={`panel-${i}`} aria-labelledby={`tab-${i}`} hidden={i !== active} style= > {i === active && tab.content} </div> ))} </div> ); } // Usage export default function App() { return ( <div style=>

Tabs Component

<Tabs tabs={[ { label: "Overview", content:

This is the Overview panel.

}, { label: "Details", content:

This is the Details panel.

}, { label: "Reviews", content:

This is the Reviews panel.

}, ]} /> </div> ); } ```
## Q. Create a reusable Accordion component in React?
Answer ```js import { useState, ReactNode } from "react"; type AccordionItem = { title: string; content: ReactNode }; function AccordionPanel({ item, isOpen, onToggle }: { item: AccordionItem; isOpen: boolean; onToggle: () => void }) { return ( <div style=> <button aria-expanded={isOpen} onClick={onToggle} style= > {item.title} <span style=>▾</span> </button> {isOpen && ( <div style=> {item.content} </div> )} </div> ); } type AccordionProps = Readonly<{ items: AccordionItem[]; allowMultiple?: boolean }>; export function Accordion({ items, allowMultiple = false }: AccordionProps) { const [openIndexes, setOpenIndexes] = useState<Set>(new Set()); const toggle = (index: number) => { setOpenIndexes((prev) => { const next = new Set(allowMultiple ? prev : new Set()); if (prev.has(index)) next.delete(index); else next.add(index); return next; }); }; return (
{items.map((item, i) => ( <AccordionPanel key={item.title} item={item} isOpen={openIndexes.has(i)} onToggle={() => toggle(i)} /> ))}
); } // Usage export default function App() { return ( <div style=>

FAQ Accordion

<Accordion allowMultiple items={[ { title: "What is React?", content:

React is a JavaScript library for building user interfaces.

}, { title: "What is JSX?", content:

JSX is a syntax extension that lets you write HTML-like markup in JavaScript.

}, { title: "What are hooks?", content:

Hooks let you use state and other React features in functional components.

}, ]} /> </div> ); } ``` </details> ## Q. Implement a `usePrevious` custom hook in React?
Answer ```js import { useRef, useEffect, useState } from "react"; function usePrevious(value: T): T | undefined { const ref = useRef<T | undefined>(undefined); useEffect(() => { ref.current = value; }); // runs after every render — captures the value from the previous render return ref.current; } // Usage export default function App() { const [count, setCount] = useState(0); const prevCount = usePrevious(count); return (

usePrevious Hook

Current: {count}

Previous: {prevCount ?? "—"}

<button onClick={() => setCount((c) => c + 1)}>Increment</button> <button onClick={() => setCount((c) => c - 1)} style=>Decrement</button>
); } ``` </details> ## Q. Implement Undo / Redo state management in React?
Answer ```js import { useReducer, useCallback } from "react"; type HistoryState = { past: T[]; present: T; future: T[] }; type HistoryAction = | { type: "SET"; payload: T } | { type: "UNDO" } | { type: "REDO" }; function historyReducer(state: HistoryState, action: HistoryAction): HistoryState { switch (action.type) { case "SET": return { past: [...state.past, state.present], present: action.payload, future: [] }; case "UNDO": { if (state.past.length === 0) return state; const previous = state.past[state.past.length - 1]; return { past: state.past.slice(0, -1), present: previous, future: [state.present, ...state.future] }; } case "REDO": { if (state.future.length === 0) return state; const next = state.future[0]; return { past: [...state.past, state.present], present: next, future: state.future.slice(1) }; } } } function useUndoRedo(initialValue: T) { const [state, dispatch] = useReducer(historyReducer, { past: [], present: initialValue, future: [] }); return { value: state.present, set: useCallback((v: T) => dispatch({ type: "SET", payload: v }), []), undo: useCallback(() => dispatch({ type: "UNDO" }), []), redo: useCallback(() => dispatch({ type: "REDO" }), []), canUndo: state.past.length > 0, canRedo: state.future.length > 0, }; } // Usage — simple text editor with undo/redo export default function App() { const { value, set, undo, redo, canUndo, canRedo } = useUndoRedo(""); return ( <div style=>

Undo / Redo Text Editor

<textarea value={value} onChange={(e) => set(e.target.value)} rows={5} style= placeholder="Start typing…" /> <div style=> <button onClick={undo} disabled={!canUndo}>↩ Undo</button> <button onClick={redo} disabled={!canRedo}>↪ Redo</button> </div> </div> ); } ``` </details> ## Q. Implement a Virtual List (windowing) for large data sets in React?
Answer ```js import { useRef, useState, useCallback } from "react"; const TOTAL_ITEMS = 100_000; const ITEM_HEIGHT = 40; const VISIBLE_COUNT = 15; const CONTAINER_HEIGHT = ITEM_HEIGHT * VISIBLE_COUNT; const items = Array.from({ length: TOTAL_ITEMS }, (_, i) => `Row ${i + 1}`); export default function VirtualList() { const [scrollTop, setScrollTop] = useState(0); const containerRef = useRef(null); const startIndex = Math.floor(scrollTop / ITEM_HEIGHT); const endIndex = Math.min(startIndex + VISIBLE_COUNT + 1, TOTAL_ITEMS); const visibleItems = items.slice(startIndex, endIndex); const totalHeight = TOTAL_ITEMS * ITEM_HEIGHT; const offsetY = startIndex * ITEM_HEIGHT; const handleScroll = useCallback((e: React.UIEvent) => { setScrollTop((e.currentTarget as HTMLDivElement).scrollTop); }, []); return (

Virtual List ({TOTAL_ITEMS.toLocaleString()} items)

<div ref={containerRef} onScroll={handleScroll} style= > {/* Spacer to represent full height */} <div style=> <div style=> {visibleItems.map((item, i) => ( <div key={startIndex + i} style= > {item}
))} </div> </div> </div> </div> ); } ``` </details> ## Q. Implement an Error Boundary component in React?
Answer ```js import { Component, ErrorInfo, ReactNode } from "react"; type ErrorBoundaryState = { hasError: boolean; error: Error | null }; class ErrorBoundary extends Component<{ children: ReactNode; fallback?: ReactNode }, ErrorBoundaryState> { state: ErrorBoundaryState = { hasError: false, error: null }; static getDerivedStateFromError(error: Error): ErrorBoundaryState { return { hasError: true, error }; } componentDidCatch(error: Error, info: ErrorInfo) { // Log to monitoring service (e.g., Sentry) in production console.error("ErrorBoundary caught:", error, info.componentStack); } render() { if (this.state.hasError) { return this.props.fallback ?? ( <div style=>

Something went wrong

<pre style=>{this.state.error?.message}</pre> <button onClick={() => this.setState({ hasError: false, error: null })}>Try again</button> </div> ); } return this.props.children; } } // Buggy child to test boundary import { useState } from "react"; function BuggyCounter() { const [count, setCount] = useState(0); if (count === 3) throw new Error("Count reached 3 — intentional crash!"); return (

Count: {count} (crashes at 3)

<button onClick={() => setCount((c) => c + 1)}>Increment</button>
); } export default function App() { return ( <div style=>

Error Boundary Demo

<ErrorBoundary fallback={<p style=>Custom fallback UI</p>}> </ErrorBoundary> </div> ); } ```
## Q. Implement a `useDebounce` and `useThrottle` custom hook in React?
Answer ```js import { useState, useEffect, useRef, useCallback } from "react"; // useDebounce — returns a debounced copy of a value function useDebounce(value: T, delay: number): T { const [debounced, setDebounced] = useState(value); useEffect(() => { const id = setTimeout(() => setDebounced(value), delay); return () => clearTimeout(id); }, [value, delay]); return debounced; } // useThrottle — limits how often a callback can fire function useThrottle<T extends unknown[]>(fn: (...args: T) => void, delay: number) { const lastCall = useRef(0); return useCallback((...args: T) => { const now = Date.now(); if (now - lastCall.current >= delay) { lastCall.current = now; fn(...args); } }, [fn, delay]); } // Usage export default function App() { const [input, setInput] = useState(""); const [throttleLog, setThrottleLog] = useState<string[]>([]); const debouncedInput = useDebounce(input, 500); const throttledLog = useThrottle( (val: string) => setThrottleLog((prev) => [...prev.slice(-4), `Throttled: "${val}"`]), 1000 ); return ( <div style=>

useDebounce & useThrottle

<input value={input} onChange={(e) => { setInput(e.target.value); throttledLog(e.target.value); }} placeholder="Type here…" style= />

Debounced (500 ms): {debouncedInput || "—"}

    {throttleLog.map((l, i) => <li key={i}>{l}</li>)}
</div> ); } ``` </details> ## Q. Build a Data Table with sorting and filtering in React?
Answer ```js import { useState, useMemo } from "react"; type Employee = { id: number; name: string; department: string; salary: number }; type SortKey = keyof Employee; type SortDir = "asc" | "desc"; const DATA: Employee[] = [ { id: 1, name: "Alice Johnson", department: "Engineering", salary: 95000 }, { id: 2, name: "Bob Smith", department: "Marketing", salary: 72000 }, { id: 3, name: "Carol White", department: "Engineering", salary: 105000 }, { id: 4, name: "David Brown", department: "HR", salary: 68000 }, { id: 5, name: "Eva Martinez", department: "Engineering", salary: 112000 }, { id: 6, name: "Frank Lee", department: "Marketing", salary: 75000 }, { id: 7, name: "Grace Kim", department: "HR", salary: 71000 }, ]; export default function DataTable() { const [filter, setFilter] = useState(""); const [sortKey, setSortKey] = useState("id"); const [sortDir, setSortDir] = useState("asc"); const handleSort = (key: SortKey) => { if (key === sortKey) setSortDir((d) => (d === "asc" ? "desc" : "asc")); else { setSortKey(key); setSortDir("asc"); } }; const tableData = useMemo(() => { const filtered = DATA.filter((row) => Object.values(row).some((v) => String(v).toLowerCase().includes(filter.toLowerCase())) ); return filtered.sort((a, b) => { const [av, bv] = [a[sortKey], b[sortKey]]; const cmp = av < bv ? -1 : av > bv ? 1 : 0; return sortDir === "asc" ? cmp : -cmp; }); }, [filter, sortKey, sortDir]); const thStyle = (key: SortKey): React.CSSProperties => ({ padding: "10px 14px", textAlign: "left", cursor: "pointer", background: "#f1f5f9", userSelect: "none", fontWeight: sortKey === key ? 700 : 400, }); return ( <div style=>

Employee Table

<input value={filter} onChange={(e) => setFilter(e.target.value)} placeholder="Filter…" style= /> <table style=> {(["id", "name", "department", "salary"] as SortKey[]).map((key) => ( <th key={key} onClick={() => handleSort(key)} style={thStyle(key)}> {key.charAt(0).toUpperCase() + key.slice(1)} {sortKey === key ? (sortDir === "asc" ? " ▲" : " ▼") : ""} </th> ))} {tableData.map((row) => ( <tr key={row.id} style=> <td style=>{row.id}</td> <td style=>{row.name}</td> <td style=>{row.department}</td> <td style=>${row.salary.toLocaleString()}</td> </tr> ))} </table> <p style=>Showing {tableData.length} / {DATA.length} rows</p> </div> ); } ``` </details> --- ## Next.js Coding Questions --- ## Q. Implement Server-Side Rendering (SSR) using an async Server Component in Next.js 13+ App Router?
Answer ```tsx // app/users/page.tsx (Server Component — default in App Router) type User = { id: number; name: string; email: string }; async function getUsers(): Promise<User[]> { const res = await fetch("https://jsonplaceholder.typicode.com/users", { cache: "no-store", // SSR — always fetch fresh data on every request }); if (!res.ok) throw new Error("Failed to fetch users"); return res.json(); } export default async function UsersPage() { const users = await getUsers(); return ( <main style=>

Users (SSR)

    {users.map((user) => ( <li key={user.id}> {user.name} — {user.email} </li> ))}
</main> ); } ``` **Key points:** - `cache: "no-store"` disables caching → fresh data on every request (SSR behavior). - `cache: "force-cache"` (default) → data fetched once and cached (SSG behavior). - `next: { revalidate: 60 }` → ISR — regenerate every 60 seconds.
## Q. Implement Incremental Static Regeneration (ISR) in Next.js?
Answer ```tsx // app/posts/page.tsx — ISR with 60-second revalidation type Post = { id: number; title: string; body: string }; async function getPosts(): Promise<Post[]> { const res = await fetch("https://jsonplaceholder.typicode.com/posts?_limit=10", { next: { revalidate: 60 }, // ISR: regenerate at most once every 60 seconds }); if (!res.ok) throw new Error("Failed to fetch posts"); return res.json(); } export default async function PostsPage() { const posts = await getPosts(); return ( <main style=>

Posts (ISR — revalidates every 60s)

<p style=>Built at: {new Date().toLocaleTimeString()}</p>
    {posts.map((post) => ( <li key={post.id} style=> {post.title} <p style=>{post.body.slice(0, 80)}…</p> </li> ))}
</main> ); } ``` ```tsx // On-demand revalidation via API route // app/api/revalidate/route.ts import { revalidatePath } from "next/cache"; import { NextRequest, NextResponse } from "next/server"; export async function POST(req: NextRequest) { const { searchParams } = new URL(req.url); const secret = searchParams.get("secret"); if (secret !== process.env.REVALIDATE_SECRET) { return NextResponse.json({ message: "Invalid secret" }, { status: 401 }); } revalidatePath("/posts"); return NextResponse.json({ revalidated: true }); } ```
## Q. Create a dynamic API route with input validation in Next.js?
Answer ```tsx // app/api/users/[id]/route.ts import { NextRequest, NextResponse } from "next/server"; type User = { id: number; name: string; email: string }; const USERS: User[] = [ { id: 1, name: "Alice Johnson", email: "alice@example.com" }, { id: 2, name: "Bob Smith", email: "bob@example.com" }, { id: 3, name: "Carol White", email: "carol@example.com" }, ]; export async function GET( _req: NextRequest, { params }: { params: { id: string } } ) { const id = Number(params.id); if (!Number.isInteger(id) || id <= 0) { return NextResponse.json({ error: "Invalid user ID" }, { status: 400 }); } const user = USERS.find((u) => u.id === id); if (!user) { return NextResponse.json({ error: "User not found" }, { status: 404 }); } return NextResponse.json(user); } export async function PUT( req: NextRequest, { params }: { params: { id: string } } ) { const id = Number(params.id); if (!Number.isInteger(id) || id <= 0) { return NextResponse.json({ error: "Invalid user ID" }, { status: 400 }); } const body = await req.json().catch(() => null); if (!body || typeof body.name !== "string" || typeof body.email !== "string") { return NextResponse.json({ error: "Invalid request body" }, { status: 400 }); } const userIndex = USERS.findIndex((u) => u.id === id); if (userIndex === -1) { return NextResponse.json({ error: "User not found" }, { status: 404 }); } USERS[userIndex] = { ...USERS[userIndex], name: body.name, email: body.email }; return NextResponse.json(USERS[userIndex]); } ```
## Q. Implement Server Actions for form submission in Next.js 13+?
Answer ```tsx // app/contact/actions.ts "use server"; type FormState = { success: boolean; message: string }; export async function submitContact( _prevState: FormState, formData: FormData ): Promise { const name = formData.get("name") as string; const email = formData.get("email") as string; const message = formData.get("message") as string; if (!name?.trim() || !email?.includes("@") || !message?.trim()) { return { success: false, message: "Please fill in all fields correctly." }; } // Simulate sending email / saving to DB await new Promise((resolve) => setTimeout(resolve, 800)); console.log("Contact received:", { name, email, message }); return { success: true, message: `Thank you, ${name}! We'll be in touch soon.` }; } ``` ```tsx // app/contact/page.tsx "use client"; import { useFormState, useFormStatus } from "react-dom"; import { submitContact } from "./actions"; function SubmitButton() { const { pending } = useFormStatus(); return ( <button type="submit" disabled={pending} style=> {pending ? "Sending…" : "Send Message"} </button> ); } const initialState = { success: false, message: "" }; export default function ContactPage() { const [state, formAction] = useFormState(submitContact, initialState); return ( <main style=>

Contact Us

{state.message && ( <p style=>{state.message}</p> )} <form action={formAction} style=> <input name="name" placeholder="Your name" required style= /> <input name="email" type="email" placeholder="Your email" required style= /> <textarea name="message" placeholder="Your message" rows={4} required style= /> </form> </main> ); } ``` </details> ## Q. Implement route-based authentication Middleware in Next.js?
Answer ```tsx // middleware.ts (project root) import { NextRequest, NextResponse } from "next/server"; const PROTECTED_ROUTES = ["/dashboard", "/profile", "/settings"]; const PUBLIC_ROUTES = ["/login", "/register"]; export function middleware(req: NextRequest) { const { pathname } = req.nextUrl; const token = req.cookies.get("auth-token")?.value; const isProtected = PROTECTED_ROUTES.some((r) => pathname.startsWith(r)); const isPublic = PUBLIC_ROUTES.some((r) => pathname.startsWith(r)); // Redirect unauthenticated users away from protected routes if (isProtected && !token) { const loginUrl = new URL("/login", req.url); loginUrl.searchParams.set("callbackUrl", pathname); return NextResponse.redirect(loginUrl); } // Redirect authenticated users away from login/register if (isPublic && token) { return NextResponse.redirect(new URL("/dashboard", req.url)); } return NextResponse.next(); } export const config = { matcher: ["/dashboard/:path*", "/profile/:path*", "/settings/:path*", "/login", "/register"], }; ``` ```tsx // app/login/page.tsx — login page that sets the cookie "use client"; import { useRouter, useSearchParams } from "next/navigation"; import { useState } from "react"; export default function LoginPage() { const router = useRouter(); const params = useSearchParams(); const [error, setError] = useState(""); const handleLogin = async (e: React.FormEvent) => { e.preventDefault(); const data = new FormData(e.currentTarget); const email = data.get("email") as string; const password = data.get("password") as string; if (email === "admin@example.com" && password === "password") { document.cookie = "auth-token=demo-token; path=/; max-age=3600"; router.push(params.get("callbackUrl") ?? "/dashboard"); } else { setError("Invalid credentials"); } }; return ( <form onSubmit={handleLogin} style=>

Login

{error && <p style=>{error}</p>} <input name="email" type="email" placeholder="Email" required style= /> <input name="password" type="password" placeholder="Password" required style= /> <button type="submit" style=>Sign In</button> </form> ); } ``` </details> --- ## Redux Toolkit Coding Questions --- ## Q. Build a Counter app using Redux Toolkit (`createSlice`, `useSelector`, `useDispatch`)?
Answer ```js // store/counterSlice.ts import { createSlice, PayloadAction } from "@reduxjs/toolkit"; type CounterState = { value: number; step: number }; const counterSlice = createSlice({ name: "counter", initialState: { value: 0, step: 1 } as CounterState, reducers: { increment: (state) => { state.value += state.step; }, decrement: (state) => { state.value -= state.step; }, reset: (state) => { state.value = 0; }, setStep: (state, action: PayloadAction) => { state.step = action.payload; }, incrementByAmount: (state, action: PayloadAction) => { state.value += action.payload; }, }, }); export const { increment, decrement, reset, setStep, incrementByAmount } = counterSlice.actions; export default counterSlice.reducer; ``` ```js // store/store.ts import { configureStore } from "@reduxjs/toolkit"; import counterReducer from "./counterSlice"; export const store = configureStore({ reducer: { counter: counterReducer } }); export type RootState = ReturnType; export type AppDispatch = typeof store.dispatch; ``` ```js // Counter.tsx import { useDispatch, useSelector } from "react-redux"; import type { RootState, AppDispatch } from "./store/store"; import { increment, decrement, reset, setStep, incrementByAmount } from "./store/counterSlice"; export default function Counter() { const { value, step } = useSelector((state: RootState) => state.counter); const dispatch = useDispatch(); return ( <div style=>

Redux Counter: {value}

<div style=> <button onClick={() => dispatch(decrement())}>-</button> <button onClick={() => dispatch(increment())}>+</button> <button onClick={() => dispatch(reset())}>Reset</button> <button onClick={() => dispatch(incrementByAmount(10))}>+10</button> </div> </div> ); } ``` ```js // App.tsx import { Provider } from "react-redux"; import { store } from "./store/store"; import Counter from "./Counter"; export default function App() { return <Provider store={store}></Provider>; } ``` </details> ## Q. Build a Shopping Cart with Redux Toolkit?
Answer ```js // store/cartSlice.ts import { createSlice, PayloadAction } from "@reduxjs/toolkit"; export type CartItem = { id: number; name: string; price: number; quantity: number }; const cartSlice = createSlice({ name: "cart", initialState: [] as CartItem[], reducers: { addItem: (state, action: PayloadAction<Omit<CartItem, "quantity">>) => { const existing = state.find((i) => i.id === action.payload.id); if (existing) { existing.quantity += 1; } else { state.push({ ...action.payload, quantity: 1 }); } }, removeItem: (state, action: PayloadAction) => state.filter((i) => i.id !== action.payload), updateQuantity: (state, action: PayloadAction<{ id: number; quantity: number }>) => { const item = state.find((i) => i.id === action.payload.id); if (item) item.quantity = Math.max(1, action.payload.quantity); }, clearCart: () => [], }, }); export const { addItem, removeItem, updateQuantity, clearCart } = cartSlice.actions; export default cartSlice.reducer; ``` ```js // store/selectors.ts import type { RootState } from "./store"; export const selectCartItems = (state: RootState) => state.cart; export const selectCartTotal = (state: RootState) => state.cart.reduce((sum, i) => sum + i.price * i.quantity, 0); export const selectCartCount = (state: RootState) => state.cart.reduce((sum, i) => sum + i.quantity, 0); ``` ```js // CartPage.tsx import { useDispatch, useSelector } from "react-redux"; import type { AppDispatch } from "./store/store"; import { addItem, removeItem, updateQuantity, clearCart } from "./store/cartSlice"; import { selectCartItems, selectCartTotal, selectCartCount } from "./store/selectors"; const PRODUCTS = [ { id: 1, name: "Laptop", price: 999 }, { id: 2, name: "Mouse", price: 29 }, { id: 3, name: "Keyboard", price: 79 }, { id: 4, name: "Monitor", price: 349 }, ]; export default function CartPage() { const dispatch = useDispatch(); const items = useSelector(selectCartItems); const total = useSelector(selectCartTotal); const count = useSelector(selectCartCount); return ( <div style=> {/* Product list */}

Products

{PRODUCTS.map((p) => ( <div key={p.id} style=> {p.name} — ${p.price} <button onClick={() => dispatch(addItem(p))}>Add to Cart</button>
))} </div> {/* Cart */}

Cart ({count} items)

{items.length === 0 ?

Your cart is empty.

: ( <> {items.map((item) => ( <div key={item.id} style=> <span style=>{item.name}</span> <input type="number" min={1} value={item.quantity} onChange={(e) => dispatch(updateQuantity({ id: item.id, quantity: Number(e.target.value) }))} style= /> ${(item.price * item.quantity).toFixed(2)} <button onClick={() => dispatch(removeItem(item.id))}>✕</button>
))} Total: ${total.toFixed(2)} <button onClick={() => dispatch(clearCart())} style=>Clear Cart</button> </> )} </div> </div> ); } ``` </details> ## Q. Fetch async data using `createAsyncThunk` in Redux Toolkit?
Answer ```js // store/postsSlice.ts import { createSlice, createAsyncThunk } from "@reduxjs/toolkit"; type Post = { id: number; title: string; body: string }; type PostsState = { data: Post[]; loading: boolean; error: string | null }; export const fetchPosts = createAsyncThunk<Post[], void, { rejectValue: string }>( "posts/fetchAll", async (_, { rejectWithValue }) => { const res = await fetch("https://jsonplaceholder.typicode.com/posts?_limit=10"); if (!res.ok) return rejectWithValue(`HTTP error ${res.status}`); return res.json() as Promise<Post[]>; } ); const postsSlice = createSlice({ name: "posts", initialState: { data: [], loading: false, error: null } as PostsState, reducers: {}, extraReducers: (builder) => { builder .addCase(fetchPosts.pending, (state) => { state.loading = true; state.error = null; }) .addCase(fetchPosts.fulfilled, (state, action) => { state.loading = false; state.data = action.payload; }) .addCase(fetchPosts.rejected, (state, action) => { state.loading = false; state.error = action.payload ?? "Unknown error"; }); }, }); export default postsSlice.reducer; ``` ```js // PostsList.tsx import { useEffect } from "react"; import { useDispatch, useSelector } from "react-redux"; import type { RootState, AppDispatch } from "./store/store"; import { fetchPosts } from "./store/postsSlice"; export default function PostsList() { const dispatch = useDispatch(); const { data, loading, error } = useSelector((state: RootState) => state.posts); useEffect(() => { dispatch(fetchPosts()); }, [dispatch]); if (loading) return

Loading posts…

; if (error) return <p style=>Error: {error}</p>; return ( <div style=>

Posts (fetched via createAsyncThunk)

    {data.map((post) => ( <li key={post.id} style=> {post.title} <p style=>{post.body.slice(0, 100)}…</p> </li> ))}
</div> ); } ``` </details> ## Q. Build a full CRUD Todo app using Redux Toolkit?
Answer ```js // store/todosSlice.ts import { createSlice, PayloadAction, createAsyncThunk } from "@reduxjs/toolkit"; export type Todo = { id: number; title: string; completed: boolean }; type TodosState = { items: Todo[]; loading: boolean; filter: "all" | "active" | "completed" }; // Seed data from API on first load export const loadTodos = createAsyncThunk("todos/load", async () => { const res = await fetch("https://jsonplaceholder.typicode.com/todos?_limit=8"); if (!res.ok) throw new Error("Failed to load todos"); return res.json() as Promise<Todo[]>; }); const todosSlice = createSlice({ name: "todos", initialState: { items: [], loading: false, filter: "all" } as TodosState, reducers: { addTodo: (state, action: PayloadAction) => { state.items.unshift({ id: Date.now(), title: action.payload, completed: false }); }, toggleTodo: (state, action: PayloadAction) => { const todo = state.items.find((t) => t.id === action.payload); if (todo) todo.completed = !todo.completed; }, deleteTodo: (state, action: PayloadAction) => { state.items = state.items.filter((t) => t.id !== action.payload); }, editTodo: (state, action: PayloadAction<{ id: number; title: string }>) => { const todo = state.items.find((t) => t.id === action.payload.id); if (todo) todo.title = action.payload.title; }, clearCompleted: (state) => { state.items = state.items.filter((t) => !t.completed); }, setFilter: (state, action: PayloadAction<TodosState["filter"]>) => { state.filter = action.payload; }, }, extraReducers: (builder) => { builder .addCase(loadTodos.pending, (state) => { state.loading = true; }) .addCase(loadTodos.fulfilled, (state, action) => { state.loading = false; state.items = action.payload; }) .addCase(loadTodos.rejected, (state) => { state.loading = false; }); }, }); export const { addTodo, toggleTodo, deleteTodo, editTodo, clearCompleted, setFilter } = todosSlice.actions; export default todosSlice.reducer; ``` ```js // TodoApp.tsx import { useEffect, useState } from "react"; import { useDispatch, useSelector } from "react-redux"; import type { RootState, AppDispatch } from "./store/store"; import { loadTodos, addTodo, toggleTodo, deleteTodo, editTodo, clearCompleted, setFilter } from "./store/todosSlice"; export default function TodoApp() { const dispatch = useDispatch(); const { items, loading, filter } = useSelector((s: RootState) => s.todos); const [input, setInput] = useState(""); const [editId, setEditId] = useState<number | null>(null); const [editText, setEditText] = useState(""); useEffect(() => { dispatch(loadTodos()); }, [dispatch]); const handleAdd = () => { const trimmed = input.trim(); if (!trimmed) return; dispatch(addTodo(trimmed)); setInput(""); }; const handleSaveEdit = (id: number) => { const trimmed = editText.trim(); if (trimmed) dispatch(editTodo({ id, title: trimmed })); setEditId(null); }; const visible = items.filter((t) => filter === "active" ? !t.completed : filter === "completed" ? t.completed : true ); if (loading) return

Loading…

; return ( <div style=>

Redux Toolkit CRUD Todo

{/* Add input */} <div style=> <input value={input} onChange={(e) => setInput(e.target.value)} onKeyDown={(e) => e.key === "Enter" && handleAdd()} placeholder="New task…" style= /> <button onClick={handleAdd}>Add</button> </div> {/* Filter */} <div style=> {(["all", "active", "completed"] as const).map((f) => ( <button key={f} onClick={() => dispatch(setFilter(f))} style=> {f.charAt(0).toUpperCase() + f.slice(1)} </button> ))} <button onClick={() => dispatch(clearCompleted())} style=> Clear Completed </button> </div> {/* List */} <ul style=> {visible.map((todo) => ( <li key={todo.id} style=> <input type="checkbox" checked={todo.completed} onChange={() => dispatch(toggleTodo(todo.id))} /> {editId === todo.id ? ( <> <input value={editText} onChange={(e) => setEditText(e.target.value)} onKeyDown={(e) => e.key === "Enter" && handleSaveEdit(todo.id)} style= autoFocus /> <button onClick={() => handleSaveEdit(todo.id)}>Save</button> <button onClick={() => setEditId(null)}>Cancel</button> </> ) : ( <> <span style=> {todo.title} </span> <button onClick={() => { setEditId(todo.id); setEditText(todo.title); }}>✏</button> <button onClick={() => dispatch(deleteTodo(todo.id))}>✕</button> </> )} </li> ))} </ul> <p style=> {items.filter((t) => !t.completed).length} item(s) remaining </p> </div> ); } ``` </details> --- ## [Sorting Articles](https://github.com/hackerrank-test/hackerrank-react-sorting-articles) Sorting Articles ## [Slideshow App](https://github.com/hackerrank-test/hackerrank-react-slideshow-app) Slideshow App ## [Catalog Viewer](https://github.com/hackerrank-test/hackerrank-react-catalog-viewer) Catalog Viewer