February 15, 2016 12:33 pm

How to create a simple app with ReactJS

React (also known as React.js or ReactJS) is an open-source JavaScript library that helps with the view layer when working with HTML data. Thus, React is only concerned with the “V” in the MVC pattern.

In this article, we will build a simple app which shows kitten gifs from an API and allows you to load a new gif whenever you click on a button. Furthermore, the total gifs that the users have seen will be stored permanently in their browser through Local Storage. In the meantime, we will show you how to create your view in React. For that purpose, we would need a class, a state, and the usage of JSX.How to create a simple app with ReactJS

JSX adds XML syntax to JavaScript. Basically, it allows us to input XML tags directly in our JavaScript for faster templating. It has a few peculiarities though. For example, instead of using the class attribute, you would have to use the className attribute.

In our simple HTML document, we add three scripts:

The first loads React, the second transforms the JSX that we write to regular JavaScript. For production, instead of using the JSXTransform you should precompile the JSX to regular JavaScript for better performance. The third script would contain our actual code – notice that we have set the type to text/jsx.

In our script, we create a class called Cats:

With the help of JSX, we can load our Cats class using XML syntax: <Cats />.In XML, empty tags (tags which do not have to be closed) need to be obligatory closed with a /. The second argument to the render method is just the element where we want the class to be rendered.

Within the Cats class, we create a method called render:

You can see that we can put XML directly in our JavaScript method. Note that we use className instead of class. In React, the event handlers are a bit different –  you can add an onClick event but adding an event handler entitled onclick would not work.

You can also see that we have a paragraph at the end of our view which shows how many gifs we have seen. It displays the numeric this.state.catsSeen property.

In React, states allow us to display different values throughout our views and change as many of them as we want whenever we want.

Therefore, we set up a method (used by React) which would determine the initial value of our state properties. Whatever properties we return in the object in that method – they will be used as default once the user first visits the application. In it, we set as default for the catsSeen property (which we display in the render method above) either zero or an integer from localStorage which would contain the number of cat gifs seen.

Finally, in the onClick handler that we set in our render method (the render method that we define for each class is executed once we call React.render() for that particular class) we update the total gifs seen, change the src attribute of the cat image and update the stored value in Local Storage. We also add a random number when updating the src attribute so that the results do not get cached.

And, voila, the app is fully functional as shown in the picture below:

app ReactJS

Tutorial Categories:

Author Ivan Dimov

Ivan is a student of IT, a freelance web designer/developer and a tech writer. He deals with both front-end and back-end stuff. Whenever he is not in front of an Internet-enabled device he is probably reading a book or traveling. You can find more about him at: http://www.dimoff.biz. facebook, twitter

Tutorial Categories: