Adding data to JSONStore collection

Setting up the React Native development environment

Follow the instructions provided in the React Native Gettings Started Page to set up your machine for React Native development.

Adding the JSONStore SDK to your React Native app

The JSONStore SDK for React Native is available as a React Native module from npm.

Getting started with a new React Native project

  1. Create a new React Native project.
     react-native init MyReactApp
    
  2. Add the Persistent Mobile Foundation SDK to your app.
     cd MyReactApp
     npm install react-native-ibm-mobilefirst-jsonstore --save
    
  3. Link all native dependencies to your app.
    react-native link
    

Adding data to a JSONStore collection

Inside your App.js you need to import the following packages:

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

There are three steps for adding data to a JSONStore collection:

  1. Creating a new collection, you can create a new collection by calling the JSONStoreCollection constructor as shown below:.
     var favourites = new JSONStoreCollection('favourites');
    
  2. Opening a collection, you won’t be able to do anything with a newly created collection unless you open it. To open the collection, call WLJSONStore’s openCollections API. See the sample code below.
    WLJSONStore.openCollections(['favourites']).then(data => { console.log(data); }).catch(err =>{ console.log(err); });
    
  3. Adding data to collection, after you have opened a collection, start data transactions inwards or outwards. You can add data to a open collection using the following 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);
     });
    
Last modified on