【发布时间】: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);
};
(不是那种状态下写的,但是还有其他的东西。)
【问题讨论】: