javascript - 如何使用 AsyncStorage 仅显示一次应用程序简介(首次运行时) - React Native

标签 javascript react-native asyncstorage

我使用这个库作为我的应用程序简介:https://github.com/Jacse/react-native-app-intro-slider

这是我的代码:

import React from 'react';
import { StyleSheet, View, Text } from 'react-native';
import {Container} from 'native-base';
import AppIntroSlider from 'react-native-app-intro-slider';
import { AntDesign } from '../../styles/variables/Icons';

export default class TestView extends React.Component {
  constructor(props) {
    super(props);
    this.state = {
      showRealApp: false,
      //To show the main page of the app
    };
  }
  _onDone = () => {
    // After user finished the intro slides. Show real app through
    // navigation or simply by controlling state
    this.setState({ showRealApp: true });
    this.props.navigation.navigate('Home');
  };
  _onSkip = () => {
    // After user skip the intro slides. Show real app through
    // navigation or simply by controlling state
    this.setState({ showRealApp: true });
    this.props.navigation.navigate('Home');
  };
    render() {
    //If false show the Intro Slides
    if (this.state.showRealApp) {
      //Real Application
      return (
        <View
          style={{
            flex: 1,
            justifyContent: 'center',
            alignItems: 'center',
            padding: 50,
          }}>
          <Text>
            This will be your screen when you click Skip from any slide or Done
            button at last
          </Text>
        </View>
      );
    } else {
      //Intro slides
      return (
        <Container>
          <AppIntroSlider
            slides={slides}
            //comming from the JsonArray below
            onDone={this._onDone}
            //Handler for the done On last slide
            showSkipButton={true}
            onSkip={this._onSkip}
            showPrevButton={true}
            prevLabel={<AntDesign name="arrowleft" size={23} />}
            nextLabel={<AntDesign name="arrowright" size={23}/>}
            doneLabel="Готово"
          />
        </Container>
      );
    }
  }
}

如何仅显示一次应用程序简介(首次运行时)并使用 AsyncStorage 将其保存到缓存?

最佳答案

您可以使用 AsyncStorage 来完成此操作API
这是一个非常快速的方法:

import React from 'react';
import { StyleSheet, View, Text, AsyncStorage, ActivityIndicator } from 'react-native';
import {Container} from 'native-base';
import AppIntroSlider from 'react-native-app-intro-slider';
import { AntDesign } from '../../styles/variables/Icons';

export default class TestView extends React.Component {
  constructor(props) {
    super(props);
    this.state = {
      showRealApp: false,
      loading: true,
      //To show the main page of the app
    };
  }

  componentDidMount() {
    AsyncStorage.getItem('first_time').then((value) => {
      this.setState({ showRealApp: !!value, loading: false });
    });
  }

  _onDone = () => {
    // After user finished the intro slides. Show real app through
    // navigation or simply by controlling state
    AsyncStorage.setItem('first_time', 'true').then(() => {
      this.setState({ showRealApp: true });
        this.props.navigation.navigate('Home');
    });
  };

  _onSkip = () => {
    // After user skip the intro slides. Show real app through
    // navigation or simply by controlling state
    AsyncStorage.setItem('first_time', 'true').then(() => {
      this.setState({ showRealApp: true });
        this.props.navigation.navigate('Home');
    });
  };

  render() {
    if (this.state.loading) return <ActivityIndicator size="large" />

    //If false show the Intro Slides
    if (this.state.showRealApp) {
      //Real Application
      return (
        <View
          style={{
            flex: 1,
            justifyContent: 'center',
            alignItems: 'center',
            padding: 50,
          }}>
          <Text>
            This will be your screen when you click Skip from any slide or Done
            button at last
          </Text>
        </View>
      );
    } else {
      //Intro slides
      return (
        <Container>
          <AppIntroSlider
            slides={slides}
            //comming from the JsonArray below
            onDone={this._onDone}
            //Handler for the done On last slide
            showSkipButton={true}
            onSkip={this._onSkip}
            showPrevButton={true}
            prevLabel={<AntDesign name="arrowleft" size={23} />}
            nextLabel={<AntDesign name="arrowright" size={23}/>}
            doneLabel="Готово"
          />
        </Container>
      );
    }
  }
}

关于javascript - 如何使用 AsyncStorage 仅显示一次应用程序简介(首次运行时) - React Native,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54657518/

相关文章:

javascript - HTTP 请求的嵌入式凭据替代方案

javascript - 如何跨应用更新保存用户数据?

javascript - AsyncStorage React Native 保存数组

android - React native——我应该使用异步存储还是 cookie?

javascript - 更好的编写读取文件/写入文件的nodejs函数的方法

javascript - 使用 JSON 文件初始化 timezoneJS

ios - 在 React Native 中链接 map

android - 在 react-native 中打开 webview 时出现 ClassNotFoundException

react-native - 无法解析模块 `@babel/runtime/helpers/interopRequireDefault`

javascript - 在异步存储中保存和删除每周计划 react native