React Assignment – 4
Basic Questions
- Create a counter app using useState to increment and decrement a number.
- Manage a boolean value in state (toggle Show/Hide text).
- Update an object in state containing {name, age} using useState.
- Add items to an array in state using the spread operator.
- Delete an item from an array (e.g., todo list) in state immutably.
- Use multiple useState hooks to manage name, email separately.
- Render different messages conditionally (e.g., “Logged In” vs “Guest”) based on state.
- Update nested object state immutably (change only user’s age).
- Debug state updates in React Developer Tools by inspecting a component.
- Store form input field values inside local state with useState.
- Use useEffect to log a message when the component first mounts.
- Add a cleanup function in useEffect to log a message on unmount.
- Fetch fake API data inside useEffect and render it.
- Use useEffect with dependency array [count] to run only when count updates.
- Create a timer with setInterval inside useEffect and clean it up.
- Use useContext to pass a theme (light/dark) through components without prop drilling.
- Create a ThemeContext and provide “dark mode” to a child component with useContext.
- Compare inline prop passing vs useContext to see the benefit of avoiding prop drilling.
- Manage both count (number) and isVisible (boolean) in the same component with useState.
- Show the difference between using no dependency array, empty dependency [], and [count] in useEffect.
Intermediate Questions
- Build a simple Todo App using useState (add, delete todos).
- Manage form input for multiple fields (name, email, age) with a single state object.
- Create a shopping cart state where items can be added/removed with immutable updates.
- Update nested arrays (e.g., users with their tasks) immutably.
- Maintain component state organization by grouping related values logically.
- Write a toggle button that changes color conditionally based on a boolean state.
- Build a counter that updates using a callback function form of setState.
- Demonstrate state batching by updating two values in the same render.
- Use ternary conditional rendering (isLoggedIn ? “Welcome” : “Login”) with state.
- Organize state by splitting into smaller independent useState hooks vs one large state object.
- Use useEffect to fetch data and update UI, then cleanly handle loading and error states.
- Demonstrate stale state bug if dependency array is omitted, fix by adding dependencies.
- Create a subscription example (e.g., window resize listener) and clean it up correctly with useEffect.
- Compare behavior of missing cleanup in useEffect vs proper cleanup.
- Nest multiple contexts (ThemeContext, UserContext) and consume them with useContext.
- Build a parent-child component where child reads data from parent via useContext
- Demonstrate prop drilling with 3 components then remove it using useContext.
- Explain dependency array rules in useEffect by experimenting with [count] vs [].
- Create a form using state hooks where submit saves input values and clears fields.
- Show the difference between using state for derived values vs computing directly in render (avoid unnecessary state).
Advanced Questions
- Build a Todo App with filters (All, Completed, Pending) using useState.
- Create a multi-step form where state persists across steps.
- Construct a shopping cart with total price calculation using array/objects in state.
- Implement a like button system (toggle likes + maintain total count) with best practices for immutability.
- Manage a complex nested state (blog posts with comments, likes) in one component using object/array state manipulation.
- Implement an API data fetcher component using useEffect and handle success, error, and loading states.
- Build a real-time clock using useEffect and cleanup interval correctly.
- Create a login flow using useContext for global authentication state across multiple components.
- Demonstrate multiple useEffect calls: one for API fetch, another for event listener, each handling dependencies separately.
- Build a Theme Switcher App using useContext where the toggle button switches all components theme globally without prop drilling.