【问题标题】:How to show App intro slider once built on expo using typescript using asyncStorage如何使用 asyncStorage 使用 typescript 在 expo 上构建应用程序介绍滑块
【发布时间】:2020-10-30 19:47:18
【问题描述】:

我正在使用 react-native-app-intro-slider 来展示我使用 typescript 在 expo 上构建的应用程序的引导。我希望滑块仅在启动期间首次显示,但似乎遇到了问题。

我对 typescript 很陌生,但我环顾四周,所有的解决方案似乎都是使用 asyncStorage 进行本机反应。

不确定我是否在这里做。

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

const data = [{...}, {...}, {...}]

AsyncStorage.setItem('showIntro', 'true')

function App() {
  { (AsyncStorage.getItem('showIntro') === 'true') ? (
      <AppIntroSlider
        renderItem={renderItem}
        data={data}
        onDone={onDone}/>
      ) : (
        <ShowApp />
      )
    }
 }

【问题讨论】:

    标签: typescript react-native expo


    【解决方案1】:

    我猜onDone 会在您的滑块完成后被调用。您可以将您的函数传递给onDone 处理程序并将screen 设置为false。

    import AsyncStorage from '@react-native-community/async-storage'
    
    const data = [{...}, {...}, {...}]
    
    const getScreen = async () => {
      return await AsyncStorage.getItem('showIntro'); 
    }
    
    function App() {
      const [showIntro, updateShowIntro] = React.useState(getScreen());
    
      const onDone = () => {
         AsyncStorage.setItem('showIntro', false).then(()=>{});
         updateShowIntro(false);
      }
    
      return (
      { showIntro ? (
          <AppIntroSlider
            renderItem={renderItem}
            data={data}
            onDone={onDone}/>
          ) : (
            <ShowApp />
          )
        }
      );
     }
    

    【讨论】:

    • 我已经有一个 onDone,只是没有显示 :) 但是 AsyncStorage.getItem('showIntro') 正在返回一个承诺,所以 'true' 永远不会匹配 下面是我的 onDone const onDone = async () = > { 等待 AsyncStorage.setItem('showIntro', 'false') }
    • 认为它已经到了那里,但仍然不起作用:(我已将 onDone 中 AsyncStorage.setItem('showIntro', false) 中的 false 更改为字符串,因为 AsyncStorage 只能存储字符串(如果我错了,请纠正我)重新启动应用程序时,仍然会出现介绍滑块
    • 在将其更新为字符串后,您是否也在返回方法中更新了showIntro === 'true'
    • 是的,没有显示。当我控制台记录 showIntro 时,它返回一个承诺
    • 我没有设置 RN,所以我无法测试,但步骤应该相似。在getScreen 中尝试控制台日志,看看它返回了什么。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-11-26
    • 2013-06-06
    • 2018-11-19
    相关资源
    最近更新 更多