最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - React-Native AsyncStorage: TypeError: Cannot read property 'setItem' of undefined - Stack Overflow

programmeradmin0浏览0评论

I'm learning react-native and trying to use AsyncStorage. I'm using this code to store the data:

_storeData = async (token) => {
    console.log("store token");
    console.log(token);
    try {
      await AsyncStorage.setItem('token', token);
    } catch (error) {
      // Error saving data
      console.log(error);
    }
  }

Calling it with: this._storeData(res.token);

But it is giving me this error:

> TypeError: Cannot read property 'setItem' of undefined
>     at LoginOrCreateForm._callee$ (blob:http://localhost:8081/21ec2746-a3c9-4c68-937f-ee50c63a6d58:138096:71)
>     at tryCatch (blob:http://localhost:8081/21ec2746-a3c9-4c68-937f-ee50c63a6d58:21408:19)
>     at Generator.invoke [as _invoke] (blob:http://localhost:8081/21ec2746-a3c9-4c68-937f-ee50c63a6d58:21583:24)
>     at Generator.prototype.(anonymous function) [as next] (blob:http://localhost:8081/21ec2746-a3c9-4c68-937f-ee50c63a6d58:21451:23)
>     at tryCatch (blob:http://localhost:8081/21ec2746-a3c9-4c68-937f-ee50c63a6d58:21408:19)
>     at invoke (blob:http://localhost:8081/21ec2746-a3c9-4c68-937f-ee50c63a6d58:21484:22)
>     at blob:http://localhost:8081/21ec2746-a3c9-4c68-937f-ee50c63a6d58:21514:13
>     at tryCallTwo (blob:http://localhost:8081/21ec2746-a3c9-4c68-937f-ee50c63a6d58:22690:7)
>     at doResolve (blob:http://localhost:8081/21ec2746-a3c9-4c68-937f-ee50c63a6d58:22854:15)
>     at new Promise (blob:http://localhost:8081/21ec2746-a3c9-4c68-937f-ee50c63a6d58:22713:5)

Could anyone help with what I am doing wrong? Thanks

I'm learning react-native and trying to use AsyncStorage. I'm using this code to store the data:

_storeData = async (token) => {
    console.log("store token");
    console.log(token);
    try {
      await AsyncStorage.setItem('token', token);
    } catch (error) {
      // Error saving data
      console.log(error);
    }
  }

Calling it with: this._storeData(res.token);

But it is giving me this error:

> TypeError: Cannot read property 'setItem' of undefined
>     at LoginOrCreateForm._callee$ (blob:http://localhost:8081/21ec2746-a3c9-4c68-937f-ee50c63a6d58:138096:71)
>     at tryCatch (blob:http://localhost:8081/21ec2746-a3c9-4c68-937f-ee50c63a6d58:21408:19)
>     at Generator.invoke [as _invoke] (blob:http://localhost:8081/21ec2746-a3c9-4c68-937f-ee50c63a6d58:21583:24)
>     at Generator.prototype.(anonymous function) [as next] (blob:http://localhost:8081/21ec2746-a3c9-4c68-937f-ee50c63a6d58:21451:23)
>     at tryCatch (blob:http://localhost:8081/21ec2746-a3c9-4c68-937f-ee50c63a6d58:21408:19)
>     at invoke (blob:http://localhost:8081/21ec2746-a3c9-4c68-937f-ee50c63a6d58:21484:22)
>     at blob:http://localhost:8081/21ec2746-a3c9-4c68-937f-ee50c63a6d58:21514:13
>     at tryCallTwo (blob:http://localhost:8081/21ec2746-a3c9-4c68-937f-ee50c63a6d58:22690:7)
>     at doResolve (blob:http://localhost:8081/21ec2746-a3c9-4c68-937f-ee50c63a6d58:22854:15)
>     at new Promise (blob:http://localhost:8081/21ec2746-a3c9-4c68-937f-ee50c63a6d58:22713:5)

Could anyone help with what I am doing wrong? Thanks

Share Improve this question asked Dec 18, 2018 at 8:43 HenryMHenryM 5,7937 gold badges54 silver badges116 bronze badges
Add a comment  | 

5 Answers 5

Reset to default 9

I was facing the same problem.

if you are using

React Native 0.60+

then follow https://react-native-async-storage.github.io/async-storage/docs/install/ to install AsyncStorage in your project.

"@react-native-async-storage/async-storage": "^1.15.17"

After installation and linking were done, import this to your project:

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

make sure that :

import AsyncStorage from '@react-native-async-storage/async-storage';✅
import { AsyncStorage } from '@react-native-async-storage/async-storage'; ❌

Just make sure you already import { AsyncStorage } from "react-native"

its because AsyncStorage is undefined,so try to import this line in top of your class:

import { AsyncStorage } from 'react-native'
yarn add @react-native-async-storage/async-storage
yarn install

import AsyncStorage from "@react-native-async-storage/async-storage";

Source:

https://react-native-async-storage.github.io/async-storage/docs/api/

React Native Functional Component: AsyncStorage Hook Method

import { useAsyncStorage } from '@react-native-community/async-storage';
---

const { getItem, setItem } = useAsyncStorage('@storage_key');

  const readItemFromStorage = async () => {
    const item = await getItem();
    setValue(item);
  };

  const writeItemToStorage = async newValue => {
    await setItem(newValue);
    setValue(newValue);
  };

Reference: https://react-native-community.github.io/async-storage/docs/api#useasyncstorage

发布评论

评论列表(0)

  1. 暂无评论