Javascript 当用户首次打开此应用时,每个用户只会显示一次(React Native)模式
我正在努力理解“异步存储”。 这对我来说很难理解,因为我现在是一名初级工程师。 但我想知道如何将一些数据保存在用户拥有的本地设备中 在哪里可以使用Javascript 当用户首次打开此应用时,每个用户只会显示一次(React Native)模式,javascript,firebase,react-native,Javascript,Firebase,React Native,我正在努力理解“异步存储”。 这对我来说很难理解,因为我现在是一名初级工程师。 但我想知道如何将一些数据保存在用户拥有的本地设备中 在哪里可以使用 _storeData = async () => { try { await AsyncStorage.setItem('@MySuperStore:key', 'I like to save it.'); } catch (error) { // Error saving data } } 及 顺便说
_storeData = async () => {
try {
await AsyncStorage.setItem('@MySuperStore:key', 'I like to save
it.');
} catch (error) {
// Error saving data
}
}
及
顺便说一句,在我的应用程序中,我尝试制作一些表单,用户可以在其中输入一些数据,但每个用户都可以在每个表单上保存这些数据,然后如果用户以前想更改文档用户输入,他可以更改它
除此之外,用户的数据在firebase firestore上。您可以使用此示例实现“打开模式首次运行应用程序”
class ModalExample扩展组件{
建造师(道具){
超级(道具)
状态={
modalVisible:错误,
};
}
setModalVisible(可见){
this.setState({modalVisible:visible});
}
CheckIfNeedOpenModel=async()=>{
试一试{
const isFirstOpen=wait AsyncStorage.getItem('IS_FIRST_OPEN');
如果(!isFirstOpen | | isFirstOpen!='true'){//检查键是否为_FIRST\u OPEN没有值或不是'true'
//isFirstOpen为null或不是“true”,因此这是应用程序首次打开
此.setModalVisible(true)
}
}捕获(错误){
//检索数据时出错
}
}
saveModalOpen=async()=>{
试一试{
等待AsyncStorage.setItem('IS_FIRST_OPEN','true');
}捕获(错误){
//保存数据时出错
}
}
onModalShow=()=>{
这个是saveModalOpen()
}
componentDidMount(){
this.checkIfNeedOpenModel()
}
render(){
返回(
{
警报。警报('模式已关闭');
}}>
你好,世界!
{
this.setModalVisible(!this.state.modalVisible);
}}>
隐藏模态
);
}
}
所以问题是“当用户首先打开此应用程序时,每个用户只会显示一次模式?”。我认为您需要在问题内容中添加此部分。谢谢您的回答。也许是因为你的支持我才完成的。顺便问一下,我想知道如何检查或刷新此状态?因为,我想在创建此应用程序时多次检查这些模式。我认为您只需要清除数据或卸载并重新安装即可再次查看模式。但最简单的方法是推荐行等待异步存储在saveModalOpen
中执行code>并在完成测试时取消注释。如果我的答案正确,请投票并打正确的分数。非常感谢你!在iOS模拟器上重置设置后,我可以这样做。但是,我不能这样评论wait AsyncStorage.setItem('IS_FIRST_OPEN','true')代码>退出。谢谢,这解决了我在读取状态时让模式闪烁/显示一秒钟的问题。感谢你的工作。
_retrieveData = async () => {
try {
const value = await AsyncStorage.getItem('TASKS');
if (value !== null) {
// We have data!!
console.log(value);
}
} catch (error) {
// Error retrieving data
}
}
class ModalExample extends Component {
constructor(props) {
super(props)
state = {
modalVisible: false,
};
}
setModalVisible(visible) {
this.setState({modalVisible: visible});
}
checkIfNeedOpenModal = async () => {
try {
const isFirstOpen = await AsyncStorage.getItem('IS_FIRST_OPEN');
if (!isFirstOpen || isFirstOpen !== 'true') { // Check if key IS_FIRST_OPEN doesnt have value or not 'true'
// isFirstOpen is null or not 'true' so this is first time app open
this.setModalVisible(true)
}
} catch (error) {
// Error retrieving data
}
}
saveModalOpen = async () => {
try {
await AsyncStorage.setItem('IS_FIRST_OPEN', 'true');
} catch (error) {
// Error saving data
}
}
onModalShow = () => {
this.saveModalOpen()
}
componentDidMount() {
this.checkIfNeedOpenModal()
}
render() {
return (
<View style={{marginTop: 22}}>
<Modal
animationType="slide"
transparent={false}
visible={this.state.modalVisible}
onShow={this.onModalShow}
onRequestClose={() => {
Alert.alert('Modal has been closed.');
}}>
<View style={{marginTop: 22}}>
<View>
<Text>Hello World!</Text>
<TouchableHighlight
onPress={() => {
this.setModalVisible(!this.state.modalVisible);
}}>
<Text>Hide Modal</Text>
</TouchableHighlight>
</View>
</View>
</Modal>
</View>
);
}
}