"Undefined is not an Object" AsyncStorage 错误

"Undefined is not an Object" error on AsyncStorage

我正在尝试保存字符串信息,但出现以下错误。

TypeError: undefined 不是一个对象(求值'_asyncStorage.AsyncStorage.setItem')

有功能代码。

import { AsyncStorage } from '@react-native-community/async-storage'
import PropTypes from 'prop-types';

export function addStack(stackID, stackName, stackCalender, stackFeedback, stackDeadline, stackStorage) {
  const stackData = ({ stackID, stackName, stackCalender, stackFeedback, stackDeadline, stackStorage });
  try {
    AsyncStorage.setItem('stack', JSON.stringify(stackData));
  } catch (error) {
    alert(error);
  }
};

addStack.propTypes = {
  stackID: PropTypes.number.isRequired,
  stackName: PropTypes.string.isRequired,
  stackCalender: PropTypes.bool.isRequired,
  stackFeedback: PropTypes.bool.isRequired,
  stackDeadline: PropTypes.bool.isRequired,
  stackStorage: PropTypes.bool.isRequired
};

并且使用该函数的触发器如下所示。

export default class StackPanel extends Component {  
  
  state = {
    stackName: '',
    calEnable: false,
    feedEnable: false,
    deadEnable: false,
    storageEnable: false,
    check: false,
  };

  Check = (checked) => {
    this.setState({ check: true });
    const date = new Date();
    const formattedDate = moment(date).format("YYYYMMDDhhmmss");
    addStack(formattedDate, this.state.stackName, this.state.calEnable, this.state.feedEnable, this.state.deadEnable, this.state.storageEnable, this.state.check);
  };

(不是那个状态写的,还有其他的。)

下面是如何使用正确的导入方法。

import AsyncStorage from '@react-native-community/async-storage';

这个 module 没有导出为 react-native,所以它不能有方括号。

像这样在 react-native 模块中使用:

import { AsyncStorage } from 'react-native';