Learning React - Ep.1
This is the 1st post in my attempt to document my journey of learning React in depth. These posts will focus less on syntax and code snippets and more on conceptual understanding and intuition. I’ve used React before, but this time I want to go beyond “I know how to use React.” I want to understand why React works the way it does. So I’m starting with the fundamentals. At its core, React is about…
This is the inaugural entry in my endeavor to meticulously chronicle my pursuit of mastering React. My aim is to delve beyond mere syntax and code snippets, instead focusing on the underlying concepts and intuition that drive the framework. React, I quickly realized, is fundamentally about constructing a tree of components whose output determines the appearance of the user interface. A component is nothing more than a function that renders what the UI should display.
When React processes such a component, it doesn't simply translate the JSX into a DOM node. Rather, it constructs a React element tree that represents the UI. For instance, consider this React component:
```javascript
function UserCard ({ name }) {
return (
div
Hello, { name }
/ div
);
}
```
Upon rendering, React generates an element tree that looks like this:
```javascript
App
Navbar
UserCard
name = 'Vidhish'
```
This hierarchical structure is crucial because it enables React to comprehend the UI and discern what changes are required in the actual DOM. The next components we encounter are props and state. Props are simply inputs passed down from a parent component, allowing for reusability and data flow through the component tree. On the other hand, state represents information that can evolve over time:
```javascript
const [ count, setCount ] = useState (0);
```
Modifying state using `setCount(count + 1)` doesn't directly alter the DOM. Instead, it signals to React that the component's state has changed. React then re-renders the component with the updated state, determining the necessary DOM updates that need to occur.
This mental model is already reshaping my approach to understanding React. The key sequence is: Components → Element Tree → Props/State → Re-render → DOM updates. My plan is to document the concepts I discover, ranging from these foundational principles to hooks, reconciliation, rendering, performance, and eventually the inner workings of React itself.
My ultimate objective isn't merely to learn React's APIs; it's to cultivate a deep mental model of what React is doing behind the scenes when our code executes. If you're also on this journey with React, what concept triggered your first profound realization?
Written by urgent.news from Dev.to's reporting — not their text. Machine-written — may contain errors; check the original before relying on it.