In React, follow a declarative, not imperative, programming approach. In the imperative approach, you tell how to accomplish a task, but in declarative it's just telling what you want. And React supports a declarative paradigm. For example, to manipulate the DOM, in React, useRef is a declarative way, but document.getElementById is not. Although the latter will do the same thing, the former adheres to React's internal caveats and React knows how to access the DOM without affecting performance. Similarly, when we simply write an HTML element in a return statement, we don't care how React do it, instead of following the document.createElement in vanilla JS. Besides, it also makes code concise, clean and easy to read. So, by using a declarative approach, we are actually adhering to React's way of doing things and get all the benefits. Cheers! #react #javascript #programming #frontend
Most beginners miss that React isn’t just a “tool,” it’s a mindset. Thinking declaratively literally changes how you write and reason about code.
Well said Ali Raza! Declarative syntax makes React so clean to work with, but knowing the imperative approach behind it gives a full picture. It really helps when moving between vanilla JS and React projects.
Well said. Many devs learn React syntax but miss the philosophy behind it. Thinking declaratively is the real skill.
A good way to practice declarative thinking is to implement a small game (Tic-Tac-Toe, or Wordle, or similar) in React. Try to come up with a minimal required state (for example, a 2D array of Xs and Os), and derive all other states from the main state. Ideally, you should be able to "play" your game just by editing that main state directly, before you even start handling user input events. Check if your implementation is declarative, by, for example, making your initial state a "winning" state, and see if your UI displays the victory message right after the inital render. If it doesn't, somewhere in the code you set your victory state imperatively instead of deriving it from your main state.