Useful Features of React_785

React.js is currently amongst the best JavaScript UI framework that a lot of web developers are finding useful in many ways. It has a higher rendering performance and this makes it one of the most sought after framework. If you are in need of a framework that will help you come up with an incredible application interface, look no further than React.js. It has a data binding and state management feature that makes it an ideal platform for creating dynamic applications.

Many people use React.js as the V in MVC. It is a platform that you can easily use for a small feature on a project that is already exists. It is a library that will give you a very simple programming model and a great performance whenever you start creating your own applications. There is a lot that one can achieve with this framework; you just have to identify some of its best features so that you can utilize them to get what you are looking for.

Previously we had covered topics on React.js like ‘Best practices for React.js‘ and ‘Resources to learn React.js‘ In this post we will cover some useful features of React.js: So lets get started with it.

JSX

This is a language that is more or less like XML language. You do not have to use React.js with JSX but JSX language is a much preferable choice for many web developers as it is a short hand that makes it easy, whenever they are writing markup for components as well as when they are binding events. Web developers will always go for an easy way out, which is why this is a great choice for many.

There is a huge difference between writing react.js documents in JSX and in JavaScript.
Examples are as below:

render: function() {
return React.DOM.a({
href: 'http://facebook.github.io/react'
}, 'React');
}

The above code will produce results that look as below:

React

When you are using JSX, you will instead use the code below for the same result:

render: function() {
return React;
}

JSX language is more or less similar to HTML but it changes the inner HTML property of a DOM element in order to achieve results. It is also a representation of the DOM that will be rendered.

This is what results to passing properties, event bindings and state to JSX objects. A good example of this is as shown:

clickHandler: function() {
console.log('Hello');
},

render: function() {
return React;
}

Sometimes this method of event binding in React.js can look like it has been done on an inline object, but what you should know is that JSX is just an interpreter which will be used to refer to an actual DOM node.

JSX is otherwise great and anyone can choose to use it as their programming syntax, as it makes writing the components much easy and quite fast.

React.js Performance

React.js is said to be a great performer. This is one of the features that makes it much better than many great platforms out there today. The reason why it does so well is because it manages a virtual DOM and not watching and updating an actual DOM on the fly of a browser.

The virtual DOM in this case is the data and not the rendered content, therefore React.js is able to determine the pieces of data that have actually changed in great detail. When you make changes on the data, the updated DOM will now be re-rendered and you can see the changes easily and on the spot. This can be done using features like requestAnimationFrame, in order to ensure that the performance of the library is at its best at all times.

React.js’ data flow in one way

React.js is designed in such a manner that it will only support data that is flowing downstream, in one flow. If the data has to flow in another direction, you will need additional features. The reason behind this is because components are supposed to be immutable and the data within them unable to change. This way, they will only listen to data that is flowing from upstream and not data that is coming from each other. That is why React.js is known for the creation of canonical data sources that will stay synchronized across the components that will pay attention to it. It is what makes it the best framework for creating highly interactive web applications.

If a certain change is made on the data upstream, the components using that particular data will automatically re-render in order to reflect the changes. That is why they have to remain in synch with the data that is flowing downstream. This style of data binding is supported by Flux in React.js, an alternative to the common MVC.

Optimization

This is a feature that you will use whenever you feel that your React.js application is kind of slow. Sometimes it can take longer than you were expecting in order to have your comment coming up on the list. Many web developers will not have this, as speed is key whenever you are creating applications. To speed things up, you are allowed to optimistically add the comment to the list. This is a trick worth learning. The code that you can use in this case is:

// tutorial20.js
var CommentBox = React.createClass({
loadCommentsFromServer: function() {
$.ajax({
url: this.props.url,
dataType: 'json',
cache: false,
success: function(data) {
this.setState({data: data});
}.bind(this),
error: function(xhr, status, err) {
console.error(this.props.url, status, err.toString());
}.bind(this)
});
},
handleCommentSubmit: function(comment) {
var comments = this.state.data;
var newComments = comments.concat([comment]);
this.setState({data: newComments});
$.ajax({
url: this.props.url,
dataType: 'json',
type: 'POST',
data: comment,
success: function(data) {
this.setState({data: data});
}.bind(this),
error: function(xhr, status, err) {
console.error(this.props.url, status, err.toString());
}.bind(this)
});
},
getInitialState: function() {
return {data: []};
},
componentDidMount: function() {
this.loadCommentsFromServer();
setInterval(this.loadCommentsFromServer, this.props.pollInterval);
},
render: function() {
return (

Comments

 

); } });

Having an easy time with a great performing framework like React.js, is the dream of many web developers. This is a framework that can give you more but only if you are able to master all its basics and useful features.