Your Web News in One Place

Help Webnuz

Referal links:

Sign up for GreenGeeks web hosting
November 16, 2016 12:00 pm

Getting Started With React and JSX

In this tutorial, we'll have a look at how to get started with creating a React app and try to understand the basics of JSX. The tutorial assumes that you have a good understanding of HTML and JavaScript.

What Is React?

React is a JavaScript library developed by Facebook to manage the user interface for web applications with ease. One of the main highlights of React is that it helps in creating manageable UI components, which makes it easier to scale large web applications. React works on the concept of Virtual DOM, where it keeps a mirror image of the actual DOM. Whenever a change occurs, React runs a diffing process, identifies the change, and updates it to the actual DOM. The concept of Virtual DOM makes app rendering faster and improves performance.

What Is JSX?

JSX is a JavaScript syntax extension which looks similar to XML. Here is an example:

JSX code looks like HTML but is actually a mix of JavaScript and HTML. The above code renders the message inside the h1 tag in the container element. JSX is faster than JavaScript since it performs optimization while compiling the source code. JSX is also preferred since it's easier to use than plain JavaScript code.

From the official docs:

JSX is a XML-like syntax extension to ECMAScript without any defined semantics. It's NOT intended to be implemented by engines or browsers. It's NOT a proposal to incorporate JSX into the ECMAScript spec itself. It's intended to be used by various preprocessors (transpilers) to transform these tokens into standard ECMAScript.

Why Use JSX?

It's is not really required to use JSX while working with React apps. You can go with the plain old JavaScript code. But using JSX has its own advantages:

1. When compared to JavaScript, it's easier to write JSX. It is as simple as opening and closing XML tags. Here is a JSX example:

Here is the compiled React code: 

2. JSX code ensures readability and makes maintainability easier.

3. JSX optimizes the code while compiling, so it runs faster compared to the equivalent JavaScript code.

Getting Started

Let's learn further by writing some JSX code and rendering it in the browser. In order to get started with creating a React app, create a simple index.html page with the following page structure:

As seen from the above code, we have referenced the react and react-dom scripts in the index.html page. We have also used the babel script reference, which would transform JSX code to react function calls. For example, consider the following JSX code:

Babel would transform the above JSX code to the required react function as shown:

Writing React Components Using JSX

Let's create a React component which would display a welcome message. Here is how the code would look:

Message is a react component which returns the above JSX code, which is then compiled to React function code using Babel. Using ReactDOM.render, we render the component inside the HTML element div main

Save the above changes and try browsing the index.html page in the browser, and you should be able to view the message Welcome to TutsPlus in the browser.

Passing Attributes to Components

Most of the time it's required to pass data to our components which would be evaluated or modified and then rendered. Let's have a look at how we can pass attributes to our components and display the data.

Suppose we want to pass a name to our Message component and display the name in our message. First, we'll add a custom attribute to our component.

The passed attribute can be read from inside the component render function using this.props on the component key. Modify the code as shown below:

Save the above changes and browse the index.html page and you should be able to see the message.

Creating a Google Map React Component Using JSX

Now that we are familiar with JSX and creating React components using JSX, let's try creating a React component for displaying Google Maps. 

Start by adding reference to the Google Maps API in the index.html.

Create a MyMap component as shown below:

Add a method called componentDidMount in the React component, and inside that method define the map-related variables as shown:

The componentDidMount method is invoked immediately after the initial rendering, and all map objects are initialized inside this method. ReactDOM.findDOMNode finds a reference to the component's DOM node and creates the map object. marker has been defined to set the marker properties like map, position, and animation.

Try rendering the map component inside the #main div.

Save the above changes and try browsing the index.html page, and you should be able to view Google Maps in action.

Wrapping It Up

In this tutorial, we saw a basic introduction to React and JSX to get you started. We saw how to create React components using JSX and also created a Google Map React Component. In the coming tutorials, we'll be focusing on some more features of React.

The source code from this tutorial is available on GitHub.

Have you tried creating a React component recently? I would love to hear your experience. Do let me know your thoughts in the comment below.


Original Link:

Share this article:    Share on Facebook
No Article Link

TutsPlus - Code

Tuts+ is a site aimed at web developers and designers offering tutorials and articles on technologies, skills and techniques to improve how you design and build websites.

More About this Source Visit TutsPlus - Code