Join egghead, unlock knowledge.

Want more egghead?

This lesson is for members. Join us? Get access to all 3,000+ tutorials + a community with expert developers around the world.

Unlock This Lesson

Already subscribed? Sign In

Autoplay

    Persist Items with React Native AsyncStorage

    Jason BrownJason Brown

    In this lesson we'll use AsyncStorage to save our items so they persist with each reload of the application. We'll show how to use the promise base interface to handle success as well as handle errors.

    react-nativeReact Native
    0.35 - 0.37
    reactReact
    ^15.0.0
    Code

    Code

    Become a Member to view code

    You must be a Member to view code

    Access all courses and lessons, track your progress, gain confidence and expertise.

    Become a Member
    and unlock code for this lesson
    Transcript

    Transcript

    00:00 We'll start by importing AsynchStorage from React native. Now, we'll create the componentWillMount lifecycle method, we'll then call asynchstorage.getItem, pass in the key of "items" to gather, which returns promise of then, which takes a function.

    00:20 It then passes in a string of jason, we'll call try and catch the error. We need this in case our JSON is unparsable, we'll say const items = JSON.parse(json); then, call this.setSource(items, items).

    00:43 Now, we'll scroll down to our setSouce, and because all of our calls go through setSource, we can say asynchStorage.setItem(items) and then JSON.Stringify the items that we get passed in.

    01:00 Now, if we go and we refresh our emulator, you can add an item, refresh again, and there it is.

    Discuss

    Discuss