← React Beginner Questions - Set 5 Q50
React Beginner Questions - Set 5
The same moment in the lifecycle, different mechanics: `componentDidMount` runs once per mount, `useEffect` runs according to its dependencies.
// Class
class Profile extends Component {
componentDidMount() { this.load(this.props.id); }
componentDidUpdate(prev) {
if (prev.id !== this.props.id) this.load(this.props.id);
}
componentWillUnmount() { this.abort?.(); }
render() { return null; }
}
// Function
function Profile({ id }) {
useEffect(() => {
const controller = new AbortController();
load(id, controller.signal);
return () => controller.abort();
}, [id]); // mount + every id change; cleanup before each re-run
return null;
}