0

我下面的代码不会运行 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,请帮助!

4

2 回答 2

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”,如果它是第一次启动,它还会在SecureStore. 状态的变化会导致重新渲染,然后显示正确的视图。

于 2019-01-15T09:32:17.323 回答
0

您需要在文件中导入以下行: import * as SecureStore from 'expo-secure-store';

于 2020-07-13T07:17:39.643 回答