javascript - React Native 状态不会改变
问题描述
我正在开发我的第一个游戏应用程序。
我想要做的是将 GreenF 传递给 Green 组件。
但是状态不会改变。我需要在函数 play() 中随时更改 GreenF;
这只是我的代码的开始,所以没有什么是完整的,只是为了你的理解。
import React, {Component} from 'react';
import {StyleSheet, Text, View,TouchableOpacity, Button} from 'react-native';
import Green from './components/Green.js'
constructor (props){
super(props);
this.state = {
greenF: false,
score: 0,
seq: [1,2,3,1,4],
playerSeq: [],
round: 1,
}
}
play() {
this.setState({ greenF: true });
}
render(){
return (
<View>
<Button
title='start'
color='black'
onPress={this.play}>
</Button>
<Green greenFlash={this.state.greenF}> </Green>
</View>
);
}
}
解决方案
您可以使用AsyncStorage
react-native-community 提供的。它是一个持久存储,您可以在其中设置和获取键值存储。参考这个:https ://github.com/react-native-community/async-storage
无论哪种方式,您都可以使用 redux 进行全局状态管理。
推荐阅读
- node.js - IIS npm 从 webconfig 启动
- android - handleWindowVisibility:令牌 android.os.BinderProxy 在第二次运行中没有活动
- python - 将旧列的值转换为新列,并将配对列的旧值用作新列中的值
- acumatica - 多选选择器未从选择器字段中选择多个项目
- postgresql - 不小心将另一个数据库恢复到“postgres”数据库如何更正
- flutter - 如何将地图列表映射到我的自定义类列表中?
- hl7-fhir - 使用类别标记问卷项目
- algorithm - 时间复杂度差异 - O(log log n) vs O(log n)
- json - Ktor Websocket 功能与 ktor 中的 ContentNeogation 功能 (JSON / GSON)
- python - 如何设置 ForeignKey 属性只知道属性名称和相关对象的 ID?