This document provides an in-depth explanation of the technical concepts mastered while building the NewsMonkey project.
In the early days of React, Class Components were the only way to manage state and lifecycle.
Code Pattern:
export default class App extends Component {
render() {
return (
<div>Hello NewsMonkey</div>
);
}
}Learning: Understanding this.state and this.props is fundamental to mastering React's component-based architecture.
The constructor is where you initialize state and bind methods.
Code Pattern:
constructor(props) {
super(props);
this.state = {
articles: [],
loading: false
};
}Learning: super(props) is mandatory to access this.props within the constructor and to inherit from React.Component.
This method is triggered immediately after the component is added to the DOM.
Learning: It is the standard place to fetch data from an API. Since it runs after the first render, it ensures the UI structure is ready before data arrives.
Unlike regular variables, updating React state requires a specific method.
Learning: this.setState is asynchronous. When called, React schedules an update, merges the new state with the old one, and triggers a re-render.
Modern JavaScript async/await makes asynchronous code look synchronous and easier to read.
Learning: We used fetch() to call the NewsAPI endpoints. We practiced handling JSON parsing and updating the state with the results.
Converted a multi-page concept into a Single Page Application (SPA).
Learning:
BrowserRouter: Keeps UI in sync with the URL.Routes: Container for all possible routes.Route: Defines a path and the component to render.- Key Trick: Using a
keyprop on the same component across different routes forces React to re-mount the component, ensuring new data is fetched for different categories.
- Props: External data passed into a component (Read-only).
- State: Internal data managed by the component (Mutable). Learning: "State is the source of truth, Props are the messengers."
Protected sensitive data from exposure.
Learning: Prefixing variables with REACT_APP_ makes them accessible in the React code via process.env. This is a standard industry practice for security.
- Infinite Scroll: Learned how to monitor scroll position and trigger "fetch more" logic.
- Loading Bar: Learned how to pass progress values from child components back to the parent
App.jsto show a global progress bar.
Happy Coding! 🚀