Derived State in React
واجهات أماميةانترفيو تقنيمتوسطآخر مرة اتسأل من سنتين
سؤال انترفيو في تروفلا (مصر) · المرحلة: انترفيو تقني · المجال: واجهات أمامية · الوظيفة: مهندس فرونت إند · الصعوبة: متوسط · اتسأل مرة واحدة، آخرها فبراير 2025
What they ask
The technical interview with the tech lead is a conversation about your background and the stack, and it is mainly React. The recurring question: "How do you handle a state change in React that depends on another state?" They give a small scenario and want you to reason about it, not recite hooks.
Example
A product list with a search box and a category dropdown. items come from the API, query and category are user input. Where does the filtered list live?
Options they expect you to compare:
- Compute during render.
const visible = items.filter(...). No extra state, always consistent, cheap for small lists. This is the default answer and they want you to say why. useMemoaround the same computation when the list is large or the filter is expensive; explain the dependency array.useEffectthat sets a second state wheneverquerychanges. Works, but causes an extra render, can go stale, and is the anti-pattern they are fishing for. Explain when it is still legitimate (syncing with something outside React).- Reset via
keywhen a subtree should restart its local state because a parent value changed (for example, a form keyed by the selected customer id). - Lift state or use a reducer when several pieces of state must change together (query changes should reset the page number).
What they look for
- Preferring derived values over duplicated state, and naming the render-cycle cost of the effect approach.
- Knowing
useReducerfor coupled updates and when to reach for a store instead. - Talking about how you tested it and what happens on slow API responses (loading, race conditions with stale results).