react.js・reactnative・redux入門

76
React.js ReactNative Redux 入入 2017/2/9 入入入入

Upload: kazuhiro-yoshimoto

Post on 14-Feb-2017

2.246 views

Category:

Technology


1 download

TRANSCRIPT

Page 1: React.js・ReactNative・Redux入門

React.js ・ ReactNative ・ Redux入門2017/2/9 吉本和弘

Page 2: React.js・ReactNative・Redux入門

自己紹介  キャリアトレック サーバサイドエンジニア ( スマホアプリも )   ・サーバサイド( Java )、 iOS(Swift) 、 Android(Java)

・ AngularJS(1 系 ) + Monaca 、 ES5  ハイブリッドアプリ・ React.js + Meteor 、 ES2015   WEB アプリ、ハイブリッドアプリ

過去の勉強会

今回の勉強会・ React.js + ReactNative + Redux 、 TypeScript    WEB アプリ、ネイティブアプリ

吉本和弘

Page 3: React.js・ReactNative・Redux入門

本日の内容React.js 、 Redux

・環境構築・ ES2015 、 TypeScript・ React.js・サンプルアプリの説明ReactNative

・環境構築・ ReactNative のコンポーネント・サンプルアプリの説明

Page 4: React.js・ReactNative・Redux入門

サンプルプログラム

Page 5: React.js・ReactNative・Redux入門

サンプルプログラム

Redux( 共通のロジック )

React.js ReactNative

Web iOS 、 Android アプリプレゼンテーション層

ビジネスロジック層

Page 6: React.js・ReactNative・Redux入門

サンプルプログラム一覧・ React.js サンプルアプリ( Todo アプリ)   https://github.com/KazuhiroYoshimoto/react-redux-todo-sample

・ ReactNative サンプルアプリ( Todo アプリ)   https://github.com/KazuhiroYoshimoto/reactNativeTodoSample

・ React.js サンプルコード (React.js の説明用 )   https://github.com/KazuhiroYoshimoto/react-todo-sample

Page 7: React.js・ReactNative・Redux入門

React.js サンプルアプリ・ React.js サンプルアプリ( Todo アプリ)   https://github.com/KazuhiroYoshimoto/react-redux-todo-sample

・ React.js サンプルコード (React.js の説明用 )   https://github.com/KazuhiroYoshimoto/react-todo-sample

◆起動方法   npm start

◆ 環境構築   git clone [ リポジトリURL]   npm i

Page 8: React.js・ReactNative・Redux入門

ReactNative サンプルアプリ

◆起動方法   npm run build   npm start

・ ReactNative サンプルアプリ( Todo アプリ)   https://github.com/KazuhiroYoshimoto/reactNativeTodoSample

◆ 環境構築   git clone [ リポジトリ URL]   npm i   node_modules/@types/redux/index.d.ts を配置    (https://github.com/reactjs/redux/blob/master/index.d.ts)

Page 9: React.js・ReactNative・Redux入門

環境構築

Page 10: React.js・ReactNative・Redux入門

・ Node モジュール   package.json・ webpack  server.js 、 webpack.config.js 、 index.html・ TypeScript   tsconfig.json

環境構築

Page 11: React.js・ReactNative・Redux入門

Node モジュール一覧開発用モジュール (development) ・ webpack  ビルドツール ・ webpack-dev-server  ローカルでビルド、サーバー起動 ・ tsloader   TypeScript を JavaScript に変換 ・ typescript  

本番用モジュール (production) ・ react   React・ react-dom   Component のレンダリング・ redux・ react-redux・型定義ファイル (@types)   

Page 12: React.js・ReactNative・Redux入門

環境構築 (Node モジュール )・ npm init

・ npm i --save react react-dom redux react-redux

・ npm i --save @types/react @types/react-dom @types/redux

  @types/react-redux

・ npm i --save-dev webpack webpack-dev-server ts-loader typescript

・ package.json を修正

Page 13: React.js・ReactNative・Redux入門

package.jsondependencies

production パッケージ本番用devDependencies

development パッケージ開発用、本番環境ではインストールされないscripts 実行スクリプト(起動など)

Page 14: React.js・ReactNative・Redux入門

package.jsonproduction development

script「 npm start 」でサーバーを起動

Page 15: React.js・ReactNative・Redux入門

webpack・ JavaScript 、 TypeScript などのビルドツール・ webpack-dev-server を使えば、 ローカル環境でビルド、サーバー起動ができる・ webpack-dev-server      Express 製のサーバーで webpack-dev-middleware を使って    webpack のビルドを行う

Page 16: React.js・ReactNative・Redux入門

webpack (環境構築)・ server.js を作成・ webpack.config.js を作成・ index.html を作成

Page 17: React.js・ReactNative・Redux入門

server.js

・ webpack-dev-server を起動・ webpack でビルド・ビルド設定は webpack.config.js・ビルドしたファイルの公開用パスは  webpack.config の output.publicPath  

Page 18: React.js・ReactNative・Redux入門

webpack.config.jswebpack のビルド設定 ・ entry   起点となるファイル ・ output   ファイルの出力先は「 /dist/bundle.js 」   公開用パスは「 /public/bundle.js 」 ・ loaders    loaders:[‘ts’]     TypsScrpit のファイルを JavaScript に変換  

Page 19: React.js・ReactNative・Redux入門

index.html 、 Index.tsx

・ Index.tsx  ReactDom によって「 <div id=“app”></div> 」  に Components がレンダリングされる

index.html

Index.tsx(webpack.config の entry)

・ index.html 「 /public/bundle.js 」が実行

・「 localhost:3000 」にアクセス 「 /index.html 」が実行

・ bundle.js   webpack.config.entry の「 Index.tsx 」が実行

Page 20: React.js・ReactNative・Redux入門

TypeScript

Page 21: React.js・ReactNative・Redux入門

TypeScript (環境構築)・ tsc -init・ tscconfig.json を書き換える

Page 22: React.js・ReactNative・Redux入門

tsconfig.json・ compilerOpsions  「 ./dist/ 」にビルドしたファイルを出力   es2015 形式で出力・ files  コンパイル対象のファイルを指定

Page 23: React.js・ReactNative・Redux入門

TypeScript・ TypeScript は最新の JS の仕様である ES2015 ( ES6 )に準拠・静的型付けやアクセス修飾子がある・型などのチェックが厳しいため、保守が必要になる大規模開発に向いてる・ TS ファイルをコンパイル(トランスパイル)して JS ファイルとして出力

Page 24: React.js・ReactNative・Redux入門

TypeScriptReact.Component の Props,State の補完、型チェックができる

DispachActions クラス

Todo クラスProps,State の型を interface で定義し、React Component にジェネリクスで渡す

Page 25: React.js・ReactNative・Redux入門

型 (TypeScript)データ型 説明number 数値string 文字列

boolean 真偽値any なんでも可能

let 変数名 : 型名let a: number = 10let b:string = ‘a’let c:boolean = true

Page 26: React.js・ReactNative・Redux入門

型定義ファイル (TypeScript)・型情報のみを記述したスクリプトファイルを参照する仕組み・ Javascript ライブラリにその型定義ファイルがあれば、  TypeScript からでも JS ライブラリが扱える・ Typings や @types(typeScript2.0 以降 ) で 型定義ファイルを管理

Page 27: React.js・ReactNative・Redux入門

ES2015 、 TypeScript( 文法 )

Page 28: React.js・ReactNative・Redux入門

ES2015 、 TypeScript の文法・クラス (ES2015)・ export 、 import(ES2015)・アロー関数 (ES2015)・スプレッド演算子 (ES2015)・ Object.assign(ES2015)・インターフェイス (TypeScript)・ジェネリクス (TypeScript)

*サンプルアプリで使っているもののみ

Page 29: React.js・ReactNative・Redux入門

クラス (ES2015)・クラス構文・ constructor  コンストラクタ。  省略した場合は引数を持たない  空の自動コンストラクタ。   TypeScript だと、  コンストラクタの引数に   public,private を付けることで  同名のプロパティを宣言し、  初期化できる

Page 30: React.js・ReactNative・Redux入門

export 、 import(ES2015)

export default  → import xxx from [ ファイル名 ]

export  → import {[ モジュール名 ]} from [ ファイル名 ]

Page 31: React.js・ReactNative・Redux入門

アロー関数 (ES2015)

function (x) {return ・・・ }

無名関数の省略記法(x) => {return ・・・ }

x => ・・・

Page 32: React.js・ReactNative・Redux入門

スプレッド演算子 (ES2015)・配列の中で配列を展開する

Page 33: React.js・ReactNative・Redux入門

Object.assign(ES2015)・オブジェクトをマージする

・オブジェクトをコピーする

Page 34: React.js・ReactNative・Redux入門

インターフェイス (TypeScript)・中身の実装を持たず、メンバーや型の定義だけ持つ

Page 35: React.js・ReactNative・Redux入門

ジェネリクス (TypeScript)

・抽象化されたデータ型を表現する・引数である型を抽象的に使う

Page 36: React.js・ReactNative・Redux入門

React.js

Page 37: React.js・ReactNative・Redux入門

React.js

・ Component・ Props・ State

Page 38: React.js・ReactNative・Redux入門

ComponentTodoListComponent

TodoComponent

TodoComponent

TodoComponent

・ TodoListComponent の中で 各要素の TodoComponent を表示

Page 39: React.js・ReactNative・Redux入門

Props 、 StateTodoListComponent   State: Todo[]

TodoComponent

Todo(Props)

タスクを Todo 型として定義

Page 40: React.js・ReactNative・Redux入門

TodoListComponent・ State にタスクを保持する   Todo 型の配列・各々のタスクを  TodoComponent に受け渡す

Page 41: React.js・ReactNative・Redux入門

TodoComponent・ Props から受け取ったタスクを 表示する   {this.props.todo.text}

Page 42: React.js・ReactNative・Redux入門

Redux

Page 43: React.js・ReactNative・Redux入門

Redux

ActionStore

Component(React) State

Dispacher

Reducer

Provider(React-Redux)* Middleware 関連は省略

Page 44: React.js・ReactNative・Redux入門

Redux要素 ファイル React or Redux レイヤー

Action ActionTypes.ts

Redux ビジネスロジック層(ReactNative と共通 )

Dispatcher Dispatcher.tsStore Store.tsState State.ts

Reducer Reducer.tsProvider Index.tsx React-Redux プレゼンテーション層

Component Component.tsx React

Page 45: React.js・ReactNative・Redux入門

Action ( ActionTypes.ts )・ Action は Store の State を変更するためのメッセージ・ Action の種別を定義する・ Action の処理は Reducer に実装する

Page 46: React.js・ReactNative・Redux入門

Dispatcher ( Dispatcher.ts )・ dispach 関数が  Action を Reducer に渡す

・ dispach 関数を呼び出す

Page 47: React.js・ReactNative・Redux入門

Store ( Store.ts )・ createStore 関数   Store に Reducer を追加する・ combileReducers 関数  複数の reducer を統合する

・ Action と Reducer をまとめるオブジェクト

Page 48: React.js・ReactNative・Redux入門

State ( State.ts )・アプリケーションの状態を保持する・ Reducer によって更新される

Page 49: React.js・ReactNative・Redux入門

Reducer ( Reducer.ts )・ state と action を受け取って  state を返す関数

・ (state,action) => state

・ Action の処理を実装

・ State を変更するときには、 新しい State データを作る ( Object.assign({},original,new) )

Page 50: React.js・ReactNative・Redux入門

Provider ( Index.ts )・ Store(Redux) と Component(React) を紐付け

-connect 関数   connect(mapStateToProps,mapDispatchToProps)(component)

mapStateToProps ・・・ state(Redux) を props に変換mapDispatchToProps ・・・ dispach の呼び出しを props に変換

Page 51: React.js・ReactNative・Redux入門

Components

Component ( Component.tsx)TodoListComponent

TodoComponent

TodoComponent

TodoComponent

TodoFormComponent・ Components の中で  TodoFormComponent と  TodoListComponent を表示・ TodoListComponent の中で 各要素の TodoComponent を表示

Page 52: React.js・ReactNative・Redux入門

Component ( Component.tsx)

Components

TodoFormComponentTodoListComponent

TodoComponent

Store(Redux)TodoList(State),Action

TodoList(State),Action

Todo(State),Action

Action

Index.tsx(React-Redux)

TodoList(State),Action

Page 53: React.js・ReactNative・Redux入門

Components ( Component.tsx)・下記のコンポーネントから構成   -TodoFormComponent   -TodoListComponent・ TodoFormComponent  に Action を受け渡す・ TodoListComponent  に State,Action を受け渡す

Page 54: React.js・ReactNative・Redux入門

TodoFormComponent ( Component.tsx )・ State に入力データを保持   (this.state.text)

・ input タグの onChange イベント  - コンポーネントの State を更新・ formの onSubmitイベント  -this.state.textを取得する  -Todoオブジェクトを作成  -ADD_TODOアクションを呼び出す

Page 55: React.js・ReactNative・Redux入門

TodoListComponent ( Component.tsx )・各々のタスクを  TodoComponent に受け渡す

・ TodoComponent に   Todo(State),Action を受け渡す

Page 56: React.js・ReactNative・Redux入門

TodoComponent ( Component.tsx)・リストの1つの列を表示

・ li タグの onClick イベント  -COMPLETE_TODO アクション を呼び出す

Page 57: React.js・ReactNative・Redux入門

React Native

Page 58: React.js・ReactNative・Redux入門

React Native・ iOS 、 Android の Native モバイルアプリケーションを 作成するための JavaScript フレームワーク・ JavaScript 、 React.js で iOS,Android アプリを作れる・ iOS 、 Android でコードを共有できる

Page 59: React.js・ReactNative・Redux入門

環境構築

Page 60: React.js・ReactNative・Redux入門

Node モジュール一覧本番用モジュール (production)・ react・ react-native・ redux・ react-redux・型定義ファイル (@types)   

Page 61: React.js・ReactNative・Redux入門

React Native (環境構築1)・ brew install node・ brew install watchman・ npm install -g react-native-cli・ react-native init [ プロジェクト名 ]

https://facebook.github.io/react-native/docs/getting-started.html#content

Page 62: React.js・ReactNative・Redux入門

React Native (環境構築 2 )・ npm i -g typescript・ npm i --save react react-native redux react-redux・ npm i --save @types/react @types/react-native @types/redux @types/react-redux・ package.json を書き換える

Page 63: React.js・ReactNative・Redux入門

React Native (環境構築 3 )・ node_modules/@types/redux/index.d.ts を配置    (https://github.com/reactjs/redux/blob/master/index.d.ts)

・ tsc -init・ tsconfig.json を書き換える・ index.ios.js 、 index.android.js を書き換える

Page 64: React.js・ReactNative・Redux入門

package.jsonproduction

scripts ・「 npm run build 」でビルド (「 --watch 」変更を検知して自動的にコンパイル)・「 npm run ios 」で実行 ( Android の場合は「 npm run android 」)

Page 65: React.js・ReactNative・Redux入門

tsconfig.json・ compilerOptions   ES2015 形式で「 build 」フォルダに出力・ files  ビルド対象は「 ./src/Index.tsx 」

Page 66: React.js・ReactNative・Redux入門

index.ios.js 、 index.android.js・「 index.ios.js 」「 index.android.js 」ファイルを呼び出す

Page 67: React.js・ReactNative・Redux入門

Redux要素 ファイル React or Redux レイヤー

Action ActionTypes.ts

Redux ビジネスロジック層Dispatcher Dispatcher.ts

Store Store.tsState State.ts

Reducer Reducer.tsProvider Index.tsx React-Redux プレゼンテーション層

Component Component.tsx React

* Component 以外は、 WEB版と同じ

Page 68: React.js・ReactNative・Redux入門

Components

Components ( Component.tsx)TodoListComponent

TodoComponent

TodoComponent

TodoComponent

TodoFormComponent・ Components の中で  TodoFormComponent と  TodoListComponent を表示

・ TodoListComponent の中で 各要素の TodoComponent を表示

Page 69: React.js・ReactNative・Redux入門

Component ( Component.tsx)・コンポーネント構成は WEB版と同じ・ ReactNative が用意している  Componet を利用して UI を構成する

Components

TodoFormComponentTodoListComponent

TodoComponent

Store(Redux)TodoList(State),Action

TodoList(State),Action

Todo(State),Action

Action

Index.tsx(React-Redux)

TodoList(State),Action

Page 70: React.js・ReactNative・Redux入門

ReactNative の Component・ View  画面を表示するコンポーネント、 html だと div のようなもの・ TextInput  テキストを入力するコンポーネント   onChangeText イベント・・・テキストの値が変わったとき・ TouchableHighlight   Toch イベントを検知するコンポーネント   onPress イベント・・・コンポーネントが押されたとき・ Button  ボタンを表現するコンポーネント   onPress イベント・・・ボタンが押されたとき

Page 71: React.js・ReactNative・Redux入門

ReactNative の Component・ ListView  リストを表現するコンポーネント const ds = new ListView.DataSource({rowHasChanged: (r1, r2) => r1 !== r2}) const dataSource = ds.cloneWithRows( [ 配列のデータ ] )

return ( <ListView dataSource={dataSource} renderRow={(data) => [1 列の View] }/> )

https://facebook.github.io/react-native/docs/listview.html

Page 72: React.js・ReactNative・Redux入門

Components(Component.tsx)

・下記のコンポーネントから構成   -TodoFormComponent   -TodoListComponent・ TodoFormComponent  に Action を受け渡す・ TodoListComponent  に State,Action を受け渡す

Page 73: React.js・ReactNative・Redux入門

TodoFormComponent(Component.tsx)・ State に入力データを保持 (this.state.text)

・ Button の onPress イベント   -this.state.text を取得する   -Todo オブジェクトを作成   -ADD_TODO アクションを呼び出す

・ TextInput の onChangeText イベント   - コンポーネントの State を更新する

Page 74: React.js・ReactNative・Redux入門

TodoListComponent(Component.tsx)

・各々のタスクを  TodoComponent に受け渡す

・ TodoComponent に   Todo(State),Action を受け渡す

Page 75: React.js・ReactNative・Redux入門

TodoComponent(Component.tsx)

・リストの1つの列を表示・ TouchableHighlight の onPress イベント  -COMPLETE_TODO アクション  を呼び出す

Page 76: React.js・ReactNative・Redux入門

参考・ Redux 、 TypeScript

https://github.com/imagepit/typescript-react-redux-todo-sample

・ ReactNative 、 TypeScripthttps://github.com/mrpatiwi/ReactNativeTS