InterviewBuddy
All interview questions

When should you use an effect?

Intermediate

Answer

Use an effect to synchronize a component with an external system, such as a subscription or browser API. Derive values during rendering when possible and handle user actions in event handlers.

Explanation

Effects may need cleanup. Include all reactive dependencies, and avoid using effects to maintain redundant derived state.

Example

const fullName = firstName + " " + lastName; // No effect needed