Frontend System Design · Lesson 20 of 86 · 4 min
Props, State and Component Lifecycle
This is how you explain data flow inside a component and justify your design choices.
In an interview, you need to articulate how a component gets its data, how it manages its own data, and how it reacts to changes. This isn't just about React syntax; it's about demonstrating you understand the core principles of unidirectional data flow and state management at a component level.
Differentiating Props and State
The first question you'll get is often a variation of "What's the difference between props and state?" Your answer needs to be immediate and precise.
Props (properties) are how a parent component passes data to a child. They are read-only. A component cannot and should not change the props it receives. Think of them as function arguments. This enforces one-way data flow, which makes the application's behavior predictable.
State is data that a component owns and manages internally. It's private and can be changed by the component itself, usually in response to user interaction or network responses. When state changes, the component re-renders.
Interview Question: "A user clicks a 'like' button. Does the button's 'liked' status belong in props or state?"
Answer: "It belongs in state. The 'liked' status is internal data that the button component itself manages. A user interaction (a click) directly changes it. The parent component doesn't need to know about it unless other parts of the UI must react to the 'like' action, in which case the state might be lifted up."
How Changes Trigger Re-renders
A component re-renders when its props or state change. This is the fundamental mechanism for updating the UI in a component-based framework.
When you call a state setter function (like setCount(1)), you aren't changing the value directly. You are telling the framework to schedule a re-render of the component with the new state value. On the next render, the component function will execute again, and the useState hook will return the new value.
The trap: Directly mutating state (e.g., user.name = 'new name') will not trigger a re-render. The framework's renderer compares object references or primitive values to detect changes. Direct mutation keeps the reference the same, so the change goes undetected. Always use the setter function.
Managing Side Effects with useEffect
Side effects are any operations that affect something outside the component's render scope: fetching data, setting up a subscription, or manually changing the DOM. The useEffect hook is the designated place for this work.
The key to using it correctly is the dependency array.
useEffect(() => { ... }, []): The empty array means the effect runs once after the initial render (mount) and the cleanup function runs once before the component is destroyed (unmount). Use this for initial data loads or subscriptions.useEffect(() => { ... }, [someValue]): The effect runs after the initial render and again anytimesomeValuechanges. Use this to react to changes in props or state.useEffect(() => { ... }): No array means the effect runs after every single render. This is almost always a bug and can easily cause infinite loops.
The trap: Forgetting the cleanup function for subscriptions, timers (setInterval), or event listeners will cause memory leaks. The function you return from useEffect is the cleanup. It runs before the component unmounts or before the effect runs again.
import React, { useState, useEffect } from 'react';
interface Post {
id: number;
title: string;
body: string;
}
function DataFetcher({ postId }: { postId: number }) {
const [post, setPost] = useState<Post | null>(null);
const [loading, setLoading] = useState(true);
const [error, setError] = useState<string | null>(null);
useEffect(() => {
// AbortController is the modern way to handle request cancellation.
const controller = new AbortController();
const signal = controller.signal;
const fetchPost = async () => {
setLoading(true);
try {
const response = await fetch(
`https://jsonplaceholder.typicode.com/posts/${postId}`,
{ signal }
);
if (!response.ok) {
throw new Error(`HTTP error! status: ${response.status}`);
}
const data: Post = await response.json();
setPost(data);
setError(null);
} catch (e: any) {
if (e.name !== 'AbortError') {
setError(e.message);
}
} finally {
setLoading(false);
}
};
fetchPost();
// The cleanup function. This runs when postId changes or the component unmounts.
return () => {
controller.abort();
};
}, [postId]); // Re-run the effect whenever postId changes.
if (loading) return <p>Loading post...</p>;
if (error) return <p>Error: {error}</p>;
return (
<div>
<h3>{post?.title}</h3>
<p>{post?.body}</p>
</div>
);
}
The Component Lifecycle in Practice
The terms "lifecycle" come from class components (componentDidMount, componentWillUnmount), but the concepts are the same with hooks. You need to be able to describe the sequence of events.
- Mount: The component is created and inserted into the DOM for the first time.
useEffectwith an empty dependency array[]runs here. - Update: The component re-renders because its props or state changed. Effects with dependencies will run if those dependencies changed.
- Unmount: The component is removed from the DOM. The cleanup function from all
useEffecthooks runs here to prevent memory leaks.
This diagram shows the flow.
Being able to walk through this flow for a given component—explaining when an API call happens, when a timer is set, and when it's cleared—is a strong signal in a system design interview.
Try it
Find a class component in one of your projects that uses componentDidMount to set up a subscription (like a setInterval or a WebSocket listener) and componentWillUnmount to tear it down. Rewrite it as a functional component using a single useEffect hook.
Next: We'll look at how to handle data at a larger scale in "Module Bundling Basics".
Lock it in — apply this lesson under interview conditions.