React Native Syntax Basics and Styling

Marcella Maki
HackerNoon.com
4 min readNov 5, 2017

--

I’ve written about installing and getting started with React Native, and how to make basic changes to the text on the initial screen (or “stage”), and now I’m going to write a little bit about some of the important syntax concepts that are different from React that I’ve discovered while working on my React Native app.

A quick refresher on a few differences between React and React Native

  1. React is a library, and React Native is a framework.
  2. A React Native app is not a mobile web app, but is a native application
  3. React Native does not use CSS (more on this a little later!)

React Native Basic Code Structure

Like React, React Native uses ES6 features including arrow functions and also uses JSX.

Here is a snippet from the React Native documentation.

The <Text></Text> is the JSX, which renders just like HTML would on the page.

With React, you can just wrap HTML in an enclosing <div> tag, for example:

In React Native, it’s critical to use JSX, rather than the HTML tags. For example, if you would like something to render as you imagine HTML would render

Then you would write it using the <Text> JSX tags:

If you don’t do this, you will get an error message, which is one of the first problems I ran into when getting started with updating my app. This error message in this case (which typically displays on the screen you are using as your view — in this case it was on my iPhone) actually was indicating that I was using HTML tags instead of the proper JSX tags.

Updating Screens

React is single-page, meaning that you aren’t really moving when you navigate around on the application, but rather different components are just showing up on the page. Coming from web applications where a new page was loaded each time you click on something (thinking about a Rails application for example, or using <a href= “#”>Link</a>) the experience is like a much smoother version of the same concept. It feels similar, just seamless and much quicker.

In React Native, however, you navigate between screens or “scenes” using React Native tools. The simplest one suggested by the documentation is React Native Navigation, which is an easy-to-use library that lets use set up screens pretty quickly. As mentioned in the documentation, you can also stack and override the routers, meaning you can make adjustments in one place without having it affect your entire program.

Styling

And finally, a quick overview of styling. (Documentation here.)

React Native does not use CSS. However, for folks who have experience using CSS or a pre-processor like LESS or SASS, teaching yourself how to do styling with React Native should be pretty simple. A lot of the syntax is similar, although the structure is a bit different.

All React Native components have the prop “style”, which allows us to make stylistic changes to the component.

The style can then be called within the component, such as on a <Text>.

The style is defined elsewhere in the file (conventionally at the bottom) using the following format

Coming Up

A deeper dive into React Native Navigator and creating a Navigation bar and forms!

Resources

--

--