Writing cross-platform apps with react native

Source: Internet
Author: User

Writing cross-platform apps with react native

React Native is a framework for writing applications that are rendered in real-time, native components of iOS and Android platforms. It is based on the React,facebook JavaScript library, in order to build the user interface, rather than the browser as the goal, it is the mobile platform for the purpose.

In other words, suppose you are a web developer, you can use react native to write clean, high-speed mobile apps, and you can experience the familiar framework and the code base of a single JavaScript.

Before that, we've heard the promise of using apps like Cordova or titanium frameworks, so what happens when you use react native to develop apps? In this article, we will explore what react native is and how it works.

And then. We'll cover what it's like to use react native to write iOS and Android apps. At the end, you'll see why react native is one of the most recommended options for your next mobile project development.

What is react Native?

Before we're ready to focus on the developer's experience, let's start by explaining what react native is. and design a little bit about how react native works.

It's react, though.

React native is a library of JavaScript for writing user interface. is typically used on the web. Developed by Facebook. and open source in 2013. React has witnessed a wide range of uses. The use of react in the field is relatively narrow, and it only focuses on itself. Interface by rendering the user interaction of your application. It is a rival to the general large MVC style library.

Developers have been flocking to react for a number of reasons. It is lightweight, and at the same time it provides an act of immediate light. Especially for high-speed changes in data. At the same time because of its own component structure, it encourages you to write more modular, reusable code.

But not the mobile side. React native indeed but React.

Here are some differences: you should use a view component instead of a div component, and you should use an image tag. Rather than the IMG tag, the vast majority of developers are kept almost identical, of course, assuming a certain objective-c or Java language knowledge. That would be better. For mobile developers, there are such wary considerations (am I doing multi-machine adaptation?). Has my touch point been set to large enough? However, React native will feel completely familiar. And it's comfortable, of course, for developers who have learned how to develop with react in a browser.

It's really react.

Brunt. Let a person to react native produce amazing feeling is react native indeed is native. The rest of the JavaScript engine for the mobile side is to package your JavaScript code into an optimized webview which, perhaps these webview also implements the interface for the behavior of some localized UI. Let's say animation. But ultimately. You are still writing a web app.

In react, a component description describes its own characteristics; react handles the rendering for you.

A clean abstraction layer separates the two functions. For web-side. In order to render the component. React uses the standard HTML tags. This same abstraction layer, generally understood as "Bridge", allows react native to trigger the API for rendering on iOS and Android, which means that in iOS, your react native component draws a real UI View. On Android, however, they will draw native View.

You will write code that looks worse than the standard JavaScript, CSS, and HTML.

It is not straightforward to compile the cost code down. React Native Get your app. Execute it using the JavaScript engine of the hosting platform. Do not block the main UI thread at the same time. In this way, you will get the effect of the native app's operation, animation, and experience without having to write code in OBJECTIVE-C or Java form. Other cross-platform app development patterns, for example. Cordova, or titanium. Never achieve a highly consistent effect with native experience or performance.

A better developer experience

React Native offers a more powerful developer experience than standard Android or iOS development, and since your application is almost entirely JavaScript-based, you will gain the benefits of a series of web development For example, you can instantly refresh your app to see changes in your code.

Compared to spending a long time waiting to build a traditional mobile app. React native feeling is a gift of heaven.

Working with multiple platforms

It's very grateful that. React native supports multiple platforms. The vast majority of React native APIs are cross-platform, so you only need to write a React native component that will work seamlessly on iOS and Android platforms.

Facebook has advertised that their ad Manager application 87% code is reused in two platforms, but I've written a flashcard application that doesn't have a bit of platform-related code. Suppose you really want to use some platform-related code – due to the different interactions between iOS and Android's two platforms. Example. Or you want to reuse the benefits of a platform's API – it's also easy, React native allows you to specify the version number of a particular platform, and for each component, you can then integrate them into your React native version number application.

Use react native to develop

Using a simple JavaScript code base to write a real iOS or Android native app looks like a mindless thing to do.

So what is the work done with react native?

Start

To start developing applications for react native. Except react native. There is also a need to install some general dependent components for iOS and Android platforms.

Detailed queries for react native sites are available.

Building react native is simple, assuming you have installed a new version of node. You can then use NPM install-g react-native-cli to install react native.

Once you have these dependent components installed, perform the React Native initialization project name and you will proactively generate a template for React Native project.

General React components

Once your development environment is ready, it's time to write some real applications.

As mentioned earlier. React native really but React, but there are a few different. The React native component looks more like a React component for a browser. But your basic building blocks have changed, such as a similar div, IMG, or a label like P.

React Native provides you with the main components, such as text or view, in the following example, the main components ScrollView, touchablehighliht and text components are used, all of which will be mapped to the Android and iOS-specified view.

Using them to create a scrolling view is straightforward with the right touch processing:

var React = require(‘react-native‘);
var { ScrollView, TouchableHighlight, Text } = React;
var TouchDemo = React.createClass({
render: function() {
return (
<ScrollView>
<TouchableHighlight onPress={() => console.log(‘pressed‘)}>
<Text>Proper Touch Handling</Text>
</TouchableHighlight>
</ScrollView>
);
},
});

If you have not handled HTML and JavaScript syntax confusion, it seems a bit confusing, react strongly suggest you use JSX, for react Native, you also have no choice. The decorations of your rendering. is to work with JavaScript to control its behavior. This is often subject to strong opposition from the novice.

But I strongly suggest you give it a chance.

Since the react native component is very similar to the react component, it is also easy to convert react to react native.

Style sheet

To make drawing simpler and more effective, and at the same time inspiring manageable styling styles, React Native implements a strict subset of CSS.

That means you don't have to learn to design your view in the way you specify the platform, but it will take you a little time to learn how to use the React native style.

The big difference is that you don't have to worry about the rules you specify. Because the inheritance of the style is quite streamlined. And react native uses the syntax of the inline style.

Here is a sample of how a stylesheet is created in react native:

var styles = StyleSheet.create({  container: {    flex: 1,    marginTop: 30  }});

Use the inline syntax. This style is used in this way:
<View style={styles.container}>
...
</View>

Start the development of the mobile end

React native a more complex area is the building of the development environment. When using react native for development. You need to have all the necessary tools for mobile development, as well as JavaScript editing tools: A text editor, and Chrome's Debugging tools.

For iOS, this means that Xcode is turned on and includes an iOS emulator at the same time. For Android, it's not Android Studio. You will be using the command line tool. Finally, you will also need to react native package execution at the same time. You can choose your favorite text editor to use to edit your JavaScript code.

The result is that you need to have a lot of tools around you. Sometimes it does feel like there are too many tools. The desktop is so cluttered that too many desktop windows are really annoying. On the one hand, at least react native does not hide the standard app development process for you.

Move to local code

React native by providing a JavaScript interface to an already existing platform API, which means you can write to the general React code. and react Native "bridge" will be responsible for heavy conversion work, but assuming that the bridge is not intact when the back of what happened?

Inevitable. With a new framework, for example react Native. There will be some APIs that will not be called, in which case the native code can be written and the interface between the host platform and the JavaScript code is communicated.

Example:

"RCTBridgeModule.h"@interface MyCustomModule : NSObject <RCTBridgeModule>@end@implementation MyCustomModuleRCT_EXPORT_MODULE();// Available as NativeModules.MyCustomModule.processStringRCT_EXPORT_METHOD(processString:(NSString *)input callback:(RCTResponseSenderBlock)callback){  callback(@[[input stringByReplacingOccurrencesOfString:@"Goodbye" withString:@"Hello"]]);}@end

And then. Use your native module from the JavaScript module. Including it is like using a different library:

varReact =require(' react-native ');var{nativemodules, Text} = React;varMessage = React.createclass ({getinitialstate () {return{text:' Goodbye world. '}; }, Componentdidmount () {NativeModules.MyCustomModule.processString (this. State.text, (text) = = {this. setState ({text});  }); }, render: function() { return (<text>{.  text}</text>); }});

You might use this, when an API you need is not supported, suppose you want to integrate the existing OC code or Java code with the react native, or you want to write some high-action functions to handle some intensive graphics processing. Happily, React native gives a very flexible way of writing code, and when you need to, use the native module, and the way is straightforward.

Even if you've never dealt with OC or Java before, writing a bridging code is a good practice to get a pleasant feel in the development of the native mobile app.

Writing cross-platform apps with react native

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.