AsyncStorage with ListView react native -
hi want make screen list can show user saved favourite. use local json file keep orginal data. , there want save favourite data on asyncstorage. asyncstorage returns promise , can't add on state. how can it?
const ds = new listview.datasource({ rowhaschanged: (r1, r2) => r1 !== r2 }); let export default class list extends react.component { constructor(props) { let resultdata = new array; super(props) const datapoem = asyncstorage.getitem('poemdb', (err, result) => { = json.parse(result) return result }); console.log this.state = { datasource: ds.clonewithrows(datapoem), counter: 1, fontloaded: false, }; }
as said, asyncstorage.getitem
returns promise, must deal promise.
need rely on response asyncstorage.getitem()
may sure handled. can deal using then
. once promise returned, state set asynchronously:
var datapoem = function(){ asyncstorage.getitem('poemdb').then((data) => { this.setstate({ datasource: ds.clonewithrows(data), }); }); }
or can deal promise using await
, concept came es7. example docs (https://facebook.github.io/react-native/docs/asyncstorage.html):
try { const value = await asyncstorage.getitem('@mysuperstore:key'); if (value !== null){ // have data!! console.log(value); } } catch (error) { // error retrieving data }
or, create own promise, , return promise using resolve
:
var getfavouritedata = new promise(function(resolve, reject){ asyncstorage.getitem().then((data) => { resolve(data); }); });
Comments
Post a Comment