4 Most Important React.js Principles You Need To Learn
An overview of the main React concepts to become a better developer
Photo by Ferenc Almasi on Unsplash
I don't care whether you're familiar with React.js in-depth or are a total newbie. It's on the rise. It's taking over. No, wait. It's already taken over. So here are the most important React.js principles to help you reach your potential and become the best React dev.
Declarative vs Imperative
In an imperative paradigm, you can directly change individual parts of your application in response to user events. You'll understand what I mean if you've used jQuery. In jQuery, you're manually accessing and changing the DOM. Though that sounds okay, large single-page applications need a lot of re-rendering and content changes. That's tough to do manually using something like jQuery.
In an imperative paradigm, it becomes difficult to see a relationship between user events and edge cases. Let's say you have an application like Discord where a user logs in, his/her profile picture shows up, and chat notifications also appear. If Discord was indeed built imperatively, it would be tough for the developers to understand the direct correlation between a user action like logging out and the change in the DOM. Debugging becomes difficult too.
But what's the problem with DOM operations? The thing is, the browser has to do two intensive operations. The first is to repaint, that is, to change an element and add it to the page. The second is to refloat, meaning recalculating the layout of the page and moving things around if required.
Hence, React takes the responsibility of managing these extensive DOM operations and leaves us to declare what we want to see on the page. We just define the state of our application in the code and React.js handles the rest.
Component Architecture
React is designed around the concept of reusable components. You can think of these as Lego blocks. You build/define them on your own, you stack them on your own, and you can take them apart on your own. This massively helps in simplifying application code. You just provide some specific data to a component, and it renders accordingly.
From a coding perspective, components are just functions, that return HTML elements. Hence, you can even reuse them, keeping your code DRY (Don't Repeat Yourself).
One-Way Data Flow
In React, data always flows from the parent components to the child components. React components are shaped in a tree-like structure. If a parent has some data, they can pass it on to their children. Unidirectional data flow is great because it keeps errors and bugs local to that component, preventing a ripple effect throughout the application. Hence, error handling is simplified.
UI Library
React just works the view or the user interface. It doesn't care how we architect our app behind the scenes. All that matters to it is the component architecture and unidirectional data flow. This greatly helps every developer, as they have a ton of freedom about how they can structure their folders, link their web pages and much more.
React is just an extensive UI library. All it does is convert front-end code into the HTML DOM. You can learn once, write anywhere. That's the real beauty of React.js. That's also why frameworks like Vue.js are getting more popular by copying the principles of React. This is also why we have React Native, Electron.js, React Desktop and React 360 (for VR apps). The basic principles of React are the most integral, irrespective of the technology stack you use. React.js is why cross-platform frameworks and applications took off.
Thanks for reading! If you liked this article, press the favourite button and consider writing a comment for some much-appreciated feedback. If you're super interested in learning more JavaScript-related stuff, check out my blog website and read more!