More Interview Questions
Part two.
Here are a few more questions that come up often, mostly about hooks and effects.
- More questions
- Hooks
- Effects
Explain useEffect
Use effect runs code after rendering, to sync with things outside React. The dependency array decides when it runs again. And the cleanup function undoes it, before the next run or when the component goes away.
| key | value |
|---|---|
| Runs | after render |
| Deps | when to re-run |
| Cleanup | undo it |
| For | outside React |
Controlled versus uncontrolled
A controlled input gets its value from state, and updates it on every change. An uncontrolled input keeps its own value, and you read it when needed, from a ref or form data.
| key | value |
|---|---|
| Controlled | value from state |
| Uncontrolled | DOM keeps it |
| Read with | ref or FormData |
| Common | controlled |
Why are hooks rules strict?
React stores each hook's data by its position in the call order. If a hook is skipped on one render, every hook after it gets the wrong data. So hooks must run in the same order, every time.
| key | value |
|---|---|
| Stored by | call order |
| Skip one | data shifts |
| So | top level only |
| Checked by | the linter |
Summary
Effects, controlled inputs, and the rules of hooks round out the essentials. Next, the final quiz.
Key takeaways- useEffect
- Controlled
- Hook order