At first, we need to register theme data in some fist lines of code in our project. May be in index.ios.js and index.android.js file. To remove duplicate in two files above, we can require a new file app.js. Our index.ios.js and index.android.js may look like:
And app.js might be:
In this file, we should see two important lines:
These lines add theme data for our project. We have two themes called “default” and “red”. In this example, we will switch between two themes at runtime.
Now, we look at the file theme/default.js:
That’s it. It only have styles data. Then, we look at the file ThemeExample.js, that uses styles from theme:
And we see an important line import {styles} from ‘react-native-theme’;. This line makes us can use styles variant.
All the code above, we can see a strange attribute of react native component. How can we use it? Let’s take a look the next section.
Yes, we use class tag usually when we’re styling our website. But in react native, it is a weird tag for a component. However we can use it with babel-plugin-react-native-css-class plugin.
Before we use it, we need to configure our transform options via .babelrc in our project directory.
This plugin will automatically transform our class tag to react native standard style tag. For example, our output of ThemeExample.js file will look like:
Yeah! Now we can use class tag as the way we use in the website.
In the previous post, we introduced how to make a test for component in react native. Today, we introduce two tools to help you write testing code much easier. react-native-babel-jest help you to configure testing enviroment quickly and enzyme help you traverse your React Native Components’ output easier.
react-native-babel-jest help you to configure testing enviroment quickly. First, install it to your project by command
Add the code below to your package.json file
The most important things on these code are scriptPreprocessor and setupEnvScriptFile. It will help you to setup testing enviroment. It includes react-native-mocking object, so you don’t need to mock it manually. Good sound!
Let’s write a component ShowComponent.js like this
And make a test file ShowComponent-test.js in folder tests
In the testing code above, we used ReactTestUtils to make a component test. But it’s hard to remember and write it. enzyme helps us to make a good deal with it. Let’s install it.
Unmock enzyme module by adding code below to your package.json file
Now, rewrite our testing code in file ShowComponent-test.js
Run test and see the result like this
We can also test with user action. Let’s create an component ActionComponent.js like this
And the test file ActionComponent-test.js in folder tests
I am looking at my testing code, it’s shorter and clearer. These tools help me to save my time. I hope it saves your time too. If you don’t want to type the code from this tutorial, just checkout from github.
Thank you!
Testing is one of the most important parts in programming. You might need to test the function, component, action… Or you might use BDD/TDD to develop your app. This tutorial will help you to solve these problems, write the react native testing, step by step. Here we go!
To test a React Native component, we will use the React library to render component. We also use babel to transform our component code while testing. At first, we need to install it.
Create a custom preprocessor called preprocessor.js to process testing for react native
Configure the Jest by add code below to package.json
React Native Component does not render to HTML DOM. So, we need a mock to do that. Create file react-native.js in folder mocks
Let’s create a show text component ShowComponent.js
Then create the test file ShowComponent-test.js in folder tests
React testing utility provide a “Simulate” to help us simulating the user actions. However, some of the actions in react native does not exist on “Simulate”. We need to mock it before testing, look at the mocks/react-native.js
The code above helps us to match onPress on react native with onClick on “Simulate”.
Let’s create an action component ActionComponent.js
And the test file ActionComponent-test.js in folder tests
This tutorial is a little long. And I hope it is helpful for you to deal with react native testing.
I put all these code together on this repository. You can check it out and run your own.
Thank you!
In the previous post, we built “Hello World” app and learned some JavaScript code using React Native platform. But what happens in the low level code, in your native project? This tutorial will take a look into it.
Work with JavaScript Server
React Native allows you to debug your JavaScript code instantly when you make a change without rebuild your app. Because it provides two ways to load JavaScript code. The fist one, it loads from an external server. So you can change your code and then tell React Native to reload it. The second one is from app’s bundle. This option is usually used for production version. Look at the file AppDelegate.m in your iOS project, you will see some lines of code like this.
Before release your app, make sure you are using JavaScript from app’s resource. It’s very important!
React Native connects JavaScript and Native Code
Look deeper inside the code of React Native, we will learn how to make a native component works on JavaScript environment. The easiest way is to look into an React Native component. We have two Objective-C files.
We can see two macros in here. RCT_EXPORT_MODULE will export your class to React Native module. And RCT_EXPORT_METHOD will export your method to React Native method. Then we can use it on JavaScript file.
Press Cmd+R while running your app in Simulator or shake your phone and select “Reload” menu. Then you can see the result.
JavaScriptCore
In this section, I will introduce about a library that makes RCT_EXPORT_MODULE, RCT_EXPORT_METHOD macros work. It’s JavaScriptCore by Apple. It helps us to answer two questions (1)How to run JavaScript in native code? and (2)How to export native code to use in JavaScript?
With the first question, we will look at two classes: JSContext and JSValue. JSContext is a JavaScript execution environment, you use a JSContext object to evaluate JavaScript scripts. JSValue is a reference to a JavaScript value, you use a JSValue object to convert value between JavaScript and Objective-C. For example.
With the second question, we use an Objective-C block to export method for JavaScript. Yes, we will call a native method from JavaScript.
These are very simple codes to make us figure out how React Native works.
Conclusion
Because React Native is an open source framework, we can learn many things from it with source code :). When we use it to build an app, we can use only JavaScript code with many libraries or mix the native and JavaScript code for better performance. Is it a good deal? Let think about it.
A year ago, Facebook announced React Native. You can see many hybrid mobile app frameworks as PhoneGap, IONIC, Mobile Angular UI and so on. But what makes React Native different?
Native components for your app. You won’t see the UIWebView wrapper although you are coding with JavaScript!
Styling your app like CSS. Really, you can build the native app with your skill on web developing.
Debugging tools instantly on your running app. So you can build and test your UI faster.
Otherwise, React Native also works with your existing native app. And you can write the wrapper to bridge from your native code to React Native JavaScript.
So, start to learn it? Here we go.
Install React Native
This tutorial guides you to install React Native on an OS X computer. At first, we need to install Xcode 7.0 or higher. It’s also available on App Store.
And then, we install React Native with this command:
That’s it! Now you can create your own app with React Native. Let’s go.
Hello React Native App
Run command to create your own app from terminal. Note that this command will create a project on your current directory.
Then, you can build and run your app on iOS Simulator.
After that, a Node.js server is running in terminal like this.
And Hello World app is running on iOS Simulator. Press Command+D then select Show Inspector. You can debug your UI instantly on running app. Amazing!
Now, we will take a look into the code.
Code structure
The source code folder of your app will look like this.
Look into the file index.ios.js. These are some pieces of code. In the header, we will see the code line
This line will enable the strict mode in JavaScript. In this mode, you should write your code extractly, all previously accepted “bad syntax” will become to real errors.
These lines import React Native’s components the we will use later in this file. Now we take a look into our component.
Our component “HelloWorld” extends from Component. We should focus on its render function. We see the JSX syntax on return command. It’s a JavaScript syntax extension that looks similar to XML. It helps you code your UI like a HTML code! These code also use style attribute for components to render. The style is declared below:
Yes, it looks quite close to CSS. It makes you easy to build and modify your UI. And the last line of code:
This line will register your component with name ‘HelloWorld’, and React Native will use it to run on your native app.
Now can edit some line on this file and see the result on iOS Simulator by press Command+D. Yes, you don’t need to rebuild your app!
And see the result:
Okay. That’s your fist simple app.
Conclusion
We have just taken a look with React Native. You should feel yourself about it. Is it really simple? And we have many things to do with it. We can also learn more React Native by its samples from GitHub.
See you next time!