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
- Create a new React Native project.
react-native init MyReactApp
- Add the Persistent Mobile Foundation SDK to your app.
cd MyReactApp npm install react-native-ibm-mobilefirst-jsonstore --save
- 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:
- Creating a new collection, you can create a new collection by calling the
JSONStoreCollection
constructor as shown below:.var favourites = new JSONStoreCollection('favourites');
- 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); });
- 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