无法在AsyncStorage中设置setItem

时间:2017-11-25 19:52:21

标签: reactjs react-native asyncstorage

我无法使用AsyncStorage class GameViewController: UIViewController, ... { var skView: SKView! // declare a property to keep a reference to the SKView override func viewDidLoad() { super.viewDidLoad() if let view = self.view as! SKView? { skView = view // assign the SKView to our new property ... } } ... func rewardBasedVideoAdDidClose(_ rewardBasedVideoAd: GADRewardBasedVideoAd) { videoAd = createVideoAd() if let gameScene = skView.scene as? GameScene { // check to see if the current scene is the game scene gameScene.revivePlayer() // revive the player } } } 。以下是我的代码。因此,我从setItem获取州名称,然后将其存储在TextInput的{​​{1}} AsyncStorage中。我收到错误:

enter image description here

onPress

我做错了什么?请帮忙。

2 个答案:

答案 0 :(得分:4)

您需要从AsyncStorage

导入react-native
import { View, Text, StyleSheet, TextInput, TouchableOpacity , AsyncStorage} from 'react-native';

您还需要保持asyncSetName功能

的绑定

取消注释此行

this.asyncSetName = this.asyncSetName.bind(this)

同样由@Chris发现,您需要将您的班级名称更改为与AsyncStorage不同

答案 1 :(得分:3)

  1. 正如@Amir所说,您需要从AsyncStorage导入react-native,并按照他的建议保留绑定。
  2. 此时您可能还想将您的课程重命名为AsyncStorage以外的其他课程
  3. 您可以选择命名AsyncStorage密钥。将@AppName放在密钥前面是很常见的,因此最终密钥最终会成为@AppName${this.state.name}。但同样,这是可选的,仅仅是惯例。