android - AsyncStorage 在 Android 上无法正常使用 React Native

标签 android react-native asyncstorage

AsyncStorage 不允许我覆盖或删除数据。

这是我的数据

const allData = [
  {
    txnId: '012',
    category: '',
    time: 'Today, 2:00pm',
    amount: 129,
    type: 'Personal',
    images: [],
  },
  {
    txnId: '011',
    category: '',
    time: 'Today, 1:00pm',
    amount: 20,
    type: 'Personal',
    images: [],
  },
  {   
    txnId: '010',
    category: '',
    time: 'Today, 10:00am',
    amount: 200,
    type: 'Personal',
    images: [],
  },
  {
    txnId: '009',
    category: '',
    time: 'Yesterday, 11:40pm',
    amount: 649,
    type: 'Personal',
    images: [],
  },
  {
    txnId: '008',
    category: '',
    time: 'Yesterday, 6:00pm',
    amount: 200,
    type: 'Personal',
    images: [],
  },
  {   
    txnId: '007',
    category: '',
    time: 'Yesterday, 11:30am',
    amount: 2200,
    type: 'Personal',
    images: [],
  },
  {
    txnId: '006',
    category: '',
    time: 'Yesterday, 09:00am',
    amount: 200,
    type: 'Personal',
    images: [],
  },
  {
    txnId: '005',
    category: '',
    time: 'Yesterday, 10:00am',
    amount: 200,
    type: 'Personal',
    images: [],
  },
  {    
    txnId: '004',
    category: '',
    time: 'Yesterday, 09:00am',
    amount: 200,
    type: 'Personal',
    images: [],
  },
  {
    txnId: '003',
    category: 'Travel',
    time: 'Today, 12:20pm',
    amount: 2300,
    type: 'Business',
    status: 'Pending',
    images: [],
  },
  {
    txnId: '002',
    category: 'Food',
    time: 'Today, 12:02pm',
    amount: 1000,
    type: 'Business',
    status: 'Rejected',
    images: [],
  },
  {
    txnId: '001',
    category: 'Food',
    time: 'Yesterday, 07:00am',
    amount: 200,
    type: 'Business',
    status: 'Approved',
    images: [],
  }
];

以下函数是我要从 AsyncStorage 中保存和检索的地方。

componentDidMount() {
AsyncStorage.getItem('allData').then(res => {
  this.setState({
    allData: JSON.parse(res),
  });
});
console.log(this.state.allData);
}

componentWillUnmount() {
AsyncStorage.removeItem('allData');
AsyncStorage.setItem('allData', JSON.stringify(this.state.allData));
BackAndroid.removeEventListener('hardwareBackPress', this.handlesBackButton);
}

setCategory(index) {
const newData = this.state.allData.map((item, i) => {
  const value = item;
  if (i === index) {
    value.type = item.type === 'Personal' ? 'Business' : 'Personal';
    value.status = item.type === 'Personal' ? '' : 'Pending';
    value.category = item.type === 'Personal' ? '' : 'Select category';
  }
  return value;
});
AsyncStorage.mergeItem('allData', JSON.stringify(newData));
this.setState({
  allData: newData,
});
console.log(this.state.allData);
}

我的渲染函数包含以下内容

return (
                <Transactions
                  allData={this.state.allData}
                  setCategory={(index, category) => {
                    const newData = this.state.allData.map((item, i) => {
                      const value = item;
                      if (index === i) {
                        value.category = category;
                      }
                      return value;
                    });
                    AsyncStorage.mergeItem('allData', JSON.stringify(newData));
                    this.setState({
                      allData: newData,
                    });
                    console.log(this.state.allData);
                  }}
                  onChange={this.setCategory.bind(this)}
                />);

state和props没有错。注释掉所有 AsyncStorage 内容并记录状态和 Prop 会得到预期的结果。

我认为这里的主要问题是由 AsyncStorage.getItem 引起的。如果没有这个方法,所有的功能和东西都会按预期工作。我尝试将它也放在 componentWillUpdate 和 componentWillMount 中,并得到了相同的结果。我做错了什么?

有人请帮助。

最佳答案

当您调用 AsyncStorage.getItem('allData') 时,您从 promise 返回的第一项是错误对象。第二项将是您想要的数据。

试试这个:

AsyncStorage.getItem('allData').then((error, res) => {
  this.setState({
    allData: JSON.parse(res),
  });
});

关于android - AsyncStorage 在 Android 上无法正常使用 React Native,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44246053/

相关文章:

android - 当我尝试导航时,我的应用程序崩溃了,似乎找不到问题

java - 单击图像时更改 ImageView 的大小

java - 在 MPAndroidChart 的折线图中绘制科学数据

reactjs - 在react-native 和 redux 上未调用 componentwillreceiveprops

android - react-native run-android 错误 : Unable to upload some APKs

javascript - AsyncStorage React Native 保存数组

Android 从最近不工作中排除

android - 第三方 API 请求在生产中失败,但在暂存中有效。如何解决这个问题?

react-native - React Native 的 AsyncStorage 可能失败的案例有哪些?

react-native - AsyncStorage 返回 [Object] [Object]