Components, hooks, rendering, effects, performance
Practise these as flashcards. One or two lines each; the ones you miss come back first.
Start freeProps are inputs passed from parent to child components and are read-only, while state is local to a component and can be changed by the component itself. Props enable data flow down the tree, and state manages internal data.
I use the useState hook. For example, const [count, setCount] = useState(0); and then call setCount(newValue) to update the state.
Keys help React identify which items have changed, been added, or removed, so it can update the DOM efficiently. Without keys, React may misbehave or rerender unnecessarily.
Hooks must be called at the top level of a React function component or custom hook, not inside loops or conditions, and only from React functions.
useEffect works and what happens if you omit the dependency array.useEffect runs after rendering and can perform side effects like fetching data or updating the DOM. If you omit the dependency array, the effect runs after every render, which can cause performance issues or infinite loops.
useMemo improve performance?useMemo caches the result of a computation and only recalculates it when its dependencies change. This prevents expensive calculations from running on every render, improving performance in components with heavy logic.
Props drilling is passing props through many intermediate components just to reach a deeply nested child. Context allows you to provide data globally so any component can access it directly, reducing unnecessary prop passing.
I use React.lazy to import the component and Suspense to provide a fallback UI while it's loading. For example: const LazyComp = React.lazy(() => import('./LazyComp')); and <Suspense fallback={<div>Loading...</div>}><LazyComp /></Suspense>.
useCallback and useMemo?useCallback returns a memoized function, while useMemo returns a memoized value. useCallback is useful for passing stable function references to child components, and useMemo is for caching expensive calculations.
I render the component, use fireEvent.click or userEvent.click on the button, and then assert the expected change in the DOM. For example, fireEvent.click(screen.getByText('Submit')) and expect(screen.getByText('Submitted')).toBeInTheDocument().
useEffect array?The effect may use stale values from previous renders, leading to bugs or unexpected behavior. It's important to include all variables used inside the effect in the dependency array.
React compares the previous and current virtual DOM trees to update only the changed parts. Keys let React match elements between renders, so it can reuse or remove DOM nodes efficiently. Without stable keys, React may reorder or recreate elements incorrectly, causing bugs.
useEffect leads to a bug and how you would fix it.