React.js and Front-End Development
React.js and Front-End Development
React has quickly become one of the most popular ways to make a front-end app. But is the hype justified? Read on and decide.
Join the DZone community and get the full member experience.Join For Free
Before React Arrived
React Isn’t a Framework
Then came React.js and it revolutionized the way web applications are developed. However, React isn’t an MVC framework unlike Angular; it is a ‘view’ only library. That is, React.js doesn’t include state managers, routers, and API managers in the core library. It may look like a limitation to you but for React developers, this is simplicity at its best because front-end development in React centers around the React ecosystem, not just the core element, which at the end of the day removes unnecessary complications.
Technically speaking, developers can expand what the core React library can do via additional libraries. Using React together with those libraries forms the React ecosystem. For example, there is Redux for state management, React Router for routing, and Axios to facilitate API interactions. Tens of thousands of such libraries are part of the React Ecosystem. Unlike Angular, templates aren’t part of the React ecosystem. React doesn’t use templates rather; instead, it uses components.
React Uses Components
This may be a no-brainer for most web developers and those looking to develop React applications. Just like not being an MVC framework has its advantages, the depreciation of templates also does. If you look closer, HTML templates restrict the set of abstractions you can include in your UIs.
There is a reason React is called the best library for building UIs. The way it approaches building user interfaces is unique yet approachable. React.js breaks UIs into independent, reusable pieces, and isolated components. This is how you define a component, ‘Welcome,’ in ES6.
Moreover, you don’t have to write down every component while building applications in React. There are many component libraries available in the React ecosystem: React Material-UI, React-Bootstrap, and React Belle are a few examples.
All these awesome UI tips may make you think, 'don’t they bog down performance of the resulting application due to extensive DOM manipulations?' That’s a valid argument but did I mention the Virtual DOM?
Note: DOM elements form the part of an application the user sees.
The Virtual DOM
If you look at the image above, the DOM has a tree-like structure and simple change on the top level may take some time to reflect on the lower levels. This may delay UI responses and ultimately add hurdles to the user experience.
Fortunately, React solves the problem with a layer of the Virtual DOM between the user layer and actual DOM. The Virtual DOM is a virtual representation of the DOM, and it stays in memory rather than the user's screen. So the question arises, what goes on the user's screen?
Note: The Virtual DOM is conspicuous by its absence in Angular.
This is where the Virtual DOM comes in. It absorbs any changes to the DOM and keeps them in memory. The algorithm then detects on which component the change was made and updates that part of the DOM. This change will be reflected on the user's screen without disturbing the other components.
In React applications, only the component changes. That means that the page remains the same regardless of user interaction. So how can you make the page discoverable to search engines?
Server-Side Rendering Your React App to Output HTML Content
React needs server-side rendering to deliver an HTML response when a user or crawler hits a page. We handle the requests on the client-side and render the React components on the server.
JSX and Final Thoughts
Frankly speaking, whether JSX is an initiative in the right direction is a matter of great debate. While it makes coding way simpler when compared with Angular, people debate that it violates the fundamental rule of programming, separation of concerns, which is a bad coding practice in the first place.
If you enjoyed this article and want to learn more about React, check out this collection of tutorials and articles on all things React.
Published at DZone with permission of Aditya Modi . See the original article here.
Opinions expressed by DZone contributors are their own.