【问题标题】:Expo SecureStore getItemExpo SecureStore getItem
【发布时间】:2019-01-14 22:15:17
【问题描述】:

我下面的代码不会运行 if 语句,只会运行 else。

SecureStore.getItemAsync('notFirstLaunch').then((value) => {
      LaunchApp(value);
    });

    const LaunchApp = function (value) {
      console.log(value);
      if (value === "true") {
        return (
          <SafeAreaView forceInset={{ bottom: 0 }} style={{ flex: 1, backgroundColor: '#E65100' }}>
              <Main />
          </SafeAreaView>
        );
      }
      else {
        SecureStore.setItemAsync('notFirstLaunch', "true");
          return (
              <Walkthrough />
          ); 
      }
  };

我的 console.log 返回 value = true 但我的 if 语句永远不会只运行 else,请帮助!

【问题讨论】:

  • "true" 将被视为字符串。尝试删除引号。
  • 它需要一个字符串作为键和值,取出引号会引发很多错误。

标签: react-native expo walkthrough


【解决方案1】:

我认为.then 块中发生的代码存在问题。我不能指望它,但在我看来,return 语句不会影响渲染。

如果我打算做你正在做的事情,这就是我将如何重构你的组件。显然,您将更改为每个用例返回的内容,而不是我放入的简单视图。

export default class App extends React.Component {

  constructor(props) {
    super(props);

    this.state = {
      notFirstLaunch: false
    }
  }

  componentDidMount() {
    this.firstLaunchCheck();
  }

  firstLaunchCheck = () => {
    SecureStore.getItemAsync('notFirstLaunch').then(value => {
      if (value !== 'true') {
        SecureStore.setItemAsync('notFirstLaunch', 'true');
      } 
      this.setState({notFirstLaunch: value === 'true'})
    });
  }

  render() {
    if (this.state.notFirstLaunch) {
      return (
        <View style={styles.container}>
          <Text>Main</Text>
        </View>
      );
    } else {
      return (
        <View style={styles.container}>
          <Text>Walkthrough</Text>
        </View>
      );
    } 
  }
}

当组件挂载时,我调用firstLaunchCheck,它会更新 notFirstLaunch 变量的状态,如果存储在SecureStore 中的值等于“true”,如果它是第一次启动它还会设置@中的值987654325@。状态的变化会导致重新渲染,然后显示正确的视图。

【讨论】:

  • 感谢您的解决方法,您对 .then 块中的代码问题是正确的。当我找到解决方案时,我会对其进行更多研究并更新,但您的解决方法已经解决了我的问题。
【解决方案2】:

您需要在文件中导入该行: 从 'expo-secure-store' 导入 * 作为 SecureStore;

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-11-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-01-30
    • 2018-12-26
    相关资源
    最近更新 更多