React Interview Questions
Part one.
React interviews tend to ask the same core questions. Let's answer the most common ones, short and clear.
- Same questions
- Core ideas
- Short answers
What is the virtual DOM?
React keeps a lightweight description of the page. When state changes, it builds a new description, compares it with the old one, and updates only what changed in the real page. That comparison is called reconciliation.
| key | value |
|---|---|
| Describe | the UI as objects |
| Compare | old vs new |
| Update | only what changed |
| Name | reconciliation |
Why do lists need keys?
Keys tell React which item is which between renders. With stable keys, React can move, add, or remove items correctly. Using the array index breaks this when the list is reordered.
| key | value |
|---|---|
| Purpose | identify items |
| Good | a stable id |
| Bad | the index, if order changes |
| Effect | correct updates |
Props versus state
Props are passed in by the parent, and are read only. State belongs to the component, and changes over time with its setter. Both cause a re-render when they change.
| key | value |
|---|---|
| Props | from the parent |
| Props are | read only |
| State | owned, changes |
| Both | trigger renders |
Summary
Virtual DOM, keys, and props versus state are the classics. Next, more interview questions.
Key takeaways- Reconciliation
- Keys
- Props vs state