AsyncStorage.GETITEM在React天然中无法正常工作



我正在尝试使用asyncstorage获取数据。每当我调用我的动作创建者 requestData 并在传递的数据上执行控制台时,我都会得到以下类似的内容。我有两个版本的getItem。属性值应可读

{" fromDate":" 20160601"," todate":" 20160701","属性":{" _ 40":0," _ 65":0," _ 55"," _ 55":NULL,_ 72" _ 72":_ 72":null}," url":"/abc/abc/xyz"}

 async getItem(item) {
  let response = await AsyncStorage.getItem(item);
  let responseJson = await JSON.stringify(response);
  return responseJson;
}

async getItem(item) {
  try {
    const value =  AsyncStorage.getItem(item).then((value) => { console.log("inside componentWillMount method call and value is "+value);
    this.setState({'assetIdList': value});
     }).then(res => {
    return res;
  });
    console.log("----------------------------value--------------------------------------"+value);
    return value;
  } catch (error) {
    // Handle errors here
    console.log("error is "+error);
  }
}
componentWillMount() {
    requestData({
      fromDate: '20160601',
      toDate: '20160701',
      assetId: this.getItem(cmn.settings.property),
      url: '/abc/abc/xyz'
    });
  }

您将属性作为承诺,您需要解决它。尝试使用某些链接。

 assetId: this.getItem(cmn.settings.property).then((res) => res)
          .catch((error) => null);

由于 AsyncStorage本质上是异步的,因此您必须等待它返回对象,然后调用您的 requestData method;如下 -

class MyComponent extends React.Component {
  componentWillMount() {
    this.retrieveFromStorageAndRequestData();  
  }      
  async getItem(item) {
    let response = await AsyncStorage.getItem(item);
    // don't need await here since JSON.stringify is synchronous
    let responseJson = JSON.stringify(response); 
    return responseJson;
  }
  async retrieveFromStorageAndRequestData = () => {
    let assetId = await getItem(cmn.settings.property);  
    requestData({
      fromDate: '20160601',
      toDate: '20160701',
      assetId,
      url: '/abc/abc/xyz'
    }) ;
  }
  // rest of the component
  render() {
    // render logic
  }
}

相关内容

  • 没有找到相关文章

最新更新