Unable to work with React Native Async Storage -


i having difficulty working react native async storage. of time code jumps off current execution , goes next line without getting results current line. getting errors of time.

i tried example-

'use strict';  var react = require('react-native'); var {   asyncstorage,   pickerios,   text,   view } = react; var pickeritemios = pickerios.item;  var storage_key = '@asyncstorageexample:key'; var colors = ['red', 'orange', 'yellow', 'green', 'blue'];  var basicstorageexample = react.createclass({   componentdidmount() {     this._loadinitialstate().done();   },    async _loadinitialstate() {     try {       var value = await asyncstorage.getitem(storage_key);       if (value !== null){         this.setstate({selectedvalue: value});         this._appendmessage('recovered selection disk: ' + value);       } else {         this._appendmessage('initialized no selection on disk.');       }     } catch (error) {       this._appendmessage('asyncstorage error: ' + error.message);     }   },    getinitialstate() {     return {       selectedvalue: colors[0],       messages: [],     };   },    render() {     var color = this.state.selectedvalue;     return (       <view>         <pickerios           selectedvalue={color}           onvaluechange={this._onvaluechange}>           {colors.map((value) => (             <pickeritemios               key={value}               value={value}               label={value}             />           ))}         </pickerios>         <text>           {'selected: '}           <text style={{color}}>             {this.state.selectedvalue}           </text>         </text>         <text>{' '}</text>         <text onpress={this._removestorage}>           press here remove storage.         </text>         <text>{' '}</text>         <text>messages:</text>         {this.state.messages.map((m) => <text key={m}>{m}</text>)}       </view>     );   },    async _onvaluechange(selectedvalue) {     this.setstate({selectedvalue});     try {       await asyncstorage.setitem(storage_key, selectedvalue);       this._appendmessage('saved selection disk: ' + selectedvalue);     } catch (error) {       this._appendmessage('asyncstorage error: ' + error.message);     }   },    async _removestorage() {     try {       await asyncstorage.removeitem(storage_key);       this._appendmessage('selection removed disk.');     } catch (error) {       this._appendmessage('asyncstorage error: ' + error.message);     }   },    _appendmessage(message) {     this.setstate({messages: this.state.messages.concat(message)});   }, });  exports.title = 'asyncstorage'; exports.description = 'asynchronous local disk storage.'; exports.examples = [   {     title: 'basics - getitem, setitem, removeitem',     render(): reactelement { return <basicstorageexample />; }   }, ]; 

this works. but, functions doesn't work expected. getting undefined.

after reading question, first thing felt "maybe" new es7, async-await. please read async-await functions in es7 realeased later year(2016).

your functions have async in nature. put keyword before function. , await has used make call function.

specifically suggest use react-native-store repo development. react native guys have told use , other implementations agreeing asyncstorage bit complex work with.


Comments

Popular posts from this blog

get url and add instance to a model with prefilled foreign key :django admin -

css - Make div keyboard-scrollable in jQuery Mobile? -

android - Keyboard hides my half of edit-text and button below it even in scroll view -