React Assignment – 8
Basic Questions
- Set up react-router-dom in a new React project. Add two routes: /home and /about.
- Create a Navbar component with Link buttons to switch between Home and About routes.
- Build a route /contact that displays a simple “Contact Us” page.
- Use useNavigate to programmatically navigate to /about when a button is clicked.
- Implement a redirect from / to /home using routing.
- Add /profile/:username route and display the username from URL params.
- Create a /products/:id dynamic route that renders “Product ID: {id}”.
- Use useEffect and fetch to retrieve user data from https://jsonplaceholder.typicode.com/users and render names.
- Replace fetch with Axios to fetch the same data.
- Create a loading state when fetching data; display “Loading…” until data arrives.
- Add error handling: show “Something went wrong” when API request fails.
- Create a route /posts that fetches and displays a list of posts.
- Use useNavigate to go back one page when pressing a “Go Back” button.
- Create a layout component (Navbar always visible, children render below using <Outlet />).
- Create a nested route under /dashboard (child routes: /dashboard/stats and /dashboard/profile).
- Display the current route path using useLocation.
- Add a “404” route that catches all unknown paths.
- Use React.memo on a component that receives props and confirm it prevents re-renders when props don’t change.
- Build a counter component and wrap it in React.memo to avoid re-renders unless count changes.
- Create two sibling components (Parent → ChildA, ChildB), memoize ChildA so it doesn’t re-render when only ChildB updates.
Intermediate Questions
- Create a Login page and a protected route /dashboard. Redirect to /login if not logged in.
- Set up nested routes: /dashboard/settings and /dashboard/profile, both under a DashboardLayout.
- Programmatically navigate to /profile/AkashSingh from a button click.
- In /user/:id, fetch user details based on the dynamic id using Axios.
- Implement query params: /search?term=react and display the query term using useSearchParams.
- Create a logout button that redirects to /login using useNavigate.
- Add an API call that fetches posts, displays them, and also shows “Loading…” and “Error” states.
- Use React Query to fetch users from https://jsonplaceholder.typicode.com/users and display them.
- Enable caching with React Query so reloading doesn’t trigger a refetch immediately.
- Fetch data for /posts and display pagination (5 posts per page with Next/Prev buttons).
- Implement infinite scroll pagination using React Query for posts.
- Add a retry mechanism in API call (Axios retry with 3 attempts on fail).
- Build a todo list where todos are fetched from an API and updated optimistically using React Query.
- Simulate slow API (use setTimeout) and show a spinner while waiting.
- Create a search input for posts that fetches filtered data each time the user types.
- Use React.memo + useCallback to prevent unnecessary re-renders in a list of items.
- Create a ProductCard component that re-renders only if props actually change.
- Compare behavior of memoized vs non-memoized child components when parent state updates.
- Use React.lazy and Suspense to lazy load a route (/about).
- Create a custom hook useApi that wraps Axios calls and returns {data, loading, error}.
Advanced Questions
- Create a role-based protected route: only admin users can open /admin, others redirect to /not-allowed.
- Implement route guards for an ecommerce app (/cart protected, /login for unauthenticated users).
- Create nested routes with multiple layouts: AppLayout, DashboardLayout, and render different sets of pages.
- Implement a master-detail view: /products shows a list, clicking a product navigates to /products/:id with dynamic details fetched from API.
- Create a paginated API call using React Query and display “Page X of Y” with navigation.
- Implement query caching: fetch API data, then navigate away and back to avoid refetch via React Query’s cache.
- Use React.memo, useMemo, and useCallback together to fully optimize a component with heavy computation.
- Build an ecommerce “Cart” page with Context + React Query (products fetched via API, cart stored in Context, revalidate on add/remove product).
- Implement dependent queries in React Query (fetch user → then fetch posts of that user).
- Build a “Performance Test App”: one large stateful parent with many memoized children, and log re-renders before/after optimization.