【问题标题】:"Undefined is not an Object" error on AsyncStorageAsyncStorage 上的“未定义不是对象”错误
【发布时间】:2021-04-18 09:17:38
【问题描述】:

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

TypeError: undefined is not an object (evaluation '_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);
  };

(不是那种状态下写的,但是还有其他的东西。)

【问题讨论】:

    标签: react-native asyncstorage


    【解决方案1】:

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

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

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

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

    import { AsyncStorage } from 'react-native';
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-01-16
      • 2015-09-07
      • 2018-07-22
      • 2021-10-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多