Blogs Hub

by AskGif | Mar 23, 2019 | Category :coding

Learning React Native, TypeScript, and Gulp

Learning React Native, TypeScript, and Gulp

<p>After working with Ionic 1.x and TypeScript for quite a while, I&rsquo;ve decided to experiment with some compiles-to-native cross-platform technologies. I first decided to start with React Native. Despite having no experience with React, getting started with React Native was relatively painless, and I was able to get TypeScript to project up and running pretty quickly. Here&rsquo;s how I did it. Feel free to skip ahead to the Github repo to see the result</p> <p>&nbsp;</p> <p>1. Run through the &ldquo;Getting Started&rdquo; tutorial by Facebook.</p> <p>First, check out &ldquo;Getting Started&rdquo; to get your system configured and a React Native app started. If you&rsquo;ve already set things up once before, just init a new project with the following:</p> <pre class="language-javascript"><code>react-native init AwesomeProject</code></pre> <p>2. Set up your TypeScript build system</p> <p>I personally chose Gulp as my build system, but you can swap it out with any build system so long as you get the same output (files compiled from TS to TSX). First, install the packages we need (swap yarn add with npm install --save if you use npm instead of yarn:</p> <pre class="language-javascript"><code>yarn gulp gulp-tslint gulp-typescript tslint typescript --dev</code></pre> <p>Here&rsquo;s my gulpfile.js. You will want to go deeper with this (i.e. cleaning your dist/ folder before building) but this will get you started.</p> <pre class="language-markup"><code>var gulp = require("gulp"); var ts = require("gulp-typescript"); var tslint = require("gulp-tslint"); var tsProject = ts.createProject("tsconfig.json", {}); var tsSrc = ["src/**/*.ts", "src/**/*.tsx"]; gulp.task("ts:build", function() { return gulp.src(tsSrc) .pipe(tslint({ formatter: "verbose" })) .pipe(tslint.report({ emitError: false })) .pipe(tsProject()) .js .pipe(gulp.dest("dist")) .on("error", function() {}); }); gulp.task("watch", function() { gulp.watch(tsSrc, ["ts:build"]); })</code></pre> <p>&nbsp;</p> <p>tsconfig.json:</p> <pre class="language-javascript"><code>{ "compilerOptions": { "target": "es2015", "module": "es2015", "jsx": "react", "outDir": "build", "rootDir": "src", "allowSyntheticDefaultImports": true, "noImplicitAny": true, "experimentalDecorators": true, "preserveConstEnums": true, "allowJs": true, "sourceMap": true, "baseUrl": ".", "paths": { "*": ["./src/*"] }, "types": [ "react", "react-native" ] } }</code></pre> <p>3. Install typings</p> <p>React Native is typings in the TypeScript 2.X @typings npm module family. Install using:</p> <pre class="language-javascript"><code>yarn add @types/react-native --dev</code></pre> <p>4. Create an src/App.tsx file</p> <p>Start writing your app! For this example, all of the code is in the src/ folder.</p> <pre class="language-javascript"><code>import React from "react"; import { Text } from "react-native"; export class App extends React.Component&lt;any, any&gt; { public render() { return ( &lt;Text&gt;Hello World&lt;/Text&gt; ); } }</code></pre> <p>5. Compile your TypeScript to dist/</p> <p>If you used my gulpfile.js, just run gulp ts:build. Otherwise, just run your build with the output going to dist/. For me, this compiled dist/app.js:</p> <pre class="language-javascript"><code>import React from "react"; import { Text } from "react-native"; export class App extends React.Component { render() { return (React.createElement(Text, null, "Hello World")); } }</code></pre> <p>6. Update your index.android.js and index.ios.js</p> <p>I updated index.android.js to import the App class from the now-compiled dist/app file:</p> <pre class="language-javascript"><code>import { AppRegistry } from "react-native"; import { App } from "./dist/app"; AppRegistry.registerComponent("myfirstreactnative", () =&gt; App);</code></pre> <p>7. You&rsquo;re done!</p> <p>That&rsquo;s it, you are now running React Native with TypeScript. Now, there are alternative ways achieve the goal of step #6 (such as changing the entry file from index.android.js to a compiled dist/ file), but I found this to be simple and effective enough. Check out the Github repo for the result.</p>

read more...