JSONStore コレクションへのデータの追加

improve this page | report issue

React Native 開発環境のセットアップ

React Native 開発用にご使用のマシンをセットアップするには、React Native の『Gettings Started』ページに記載されている手順に従います。

React Native アプリケーションへの JSONStore SDK の追加

React Native 用の JSONStore SDK は、npm から React Native モジュールとして入手可能です。

新規 React Native プロジェクトの開始

  1. 新規 React Native プロジェクトを作成します。
     react-native init MyReactApp
    
  2. MobileFirst SDK をアプリケーションに追加します。
     cd MyReactApp
     npm install react-native-ibm-mobilefirst-jsonstore --save
    
  3. すべてのネイティブ依存関係をアプリケーションにリンクします。
    react-native link
    

JSONStore コレクションへのデータの追加

App.js 内で以下のパッケージをインポートする必要があります。

import { JSONStoreCollection, WLJSONStore } from 'react-native-ibm-mobilefirst-jsonstore';

JSONStore コレクションにデータを追加するためのステップは以下の 3 つです。

  1. 新規コレクションの作成。以下に示すように、JSONStoreCollection コンストラクターを呼び出すことで新規コレクションを作成できます。
     var favourites = new JSONStoreCollection('favourites');
    
  2. コレクションのオープン。新規作成したコレクションで何かを行うには、そのコレクションを開く必要があります。 コレクションを開くには、WLJSONStore の openCollections API を呼び出します。 以下のサンプル・コードを参照してください。
    WLJSONStore.openCollections(['favourites']).then(data => { console.log(data); }).catch(err =>{ console.log(err); });
    
  3. コレクションへのデータの追加。コレクションを開いた後で、内側または外側からデータ・トランザクションを開始します。 以下の API を使用して、開いたコレクションにデータを追加できます。
     var favCollection = new JSONStoreCollection('favourites');
     favCollection.addData(myJsonData)
     .then(data => {
     	console.log("Succesfully added data to collection!"));
     .catch(err => {
     	console.log("Error while adding data to collection. Reason : " + err);
     });
    
Inclusive terminology note: The Mobile First Platform team is making changes to support the IBM® initiative to replace racially biased and other discriminatory language in our code and content with more inclusive language. While IBM values the use of inclusive language, terms that are outside of IBM's direct influence are sometimes required for the sake of maintaining user understanding. As other industry leaders join IBM in embracing the use of inclusive language, IBM will continue to update the documentation to reflect those changes.
Last modified on February 28, 2020