【问题标题】:react native : There is way to detect if it is the first time launch of the app or not?react native:有没有办法检测是否是第一次启动应用程序?
【发布时间】:2020-06-25 09:56:21
【问题描述】:

有没有办法检测是否是第一次启动应用程序? 仅当它是不是第一次打开应用程序时返回“AppNavigator”否则返回所有内容。 这是我的示例代码: 如何在我的代码中使用 AsyncStorage 示例?

///////// AsyncStorage/////////////

async componentDidMount() {
  const firstTime = await AsyncStorage.getItem("isFirstTime")
  if(firstTime != null) {
    // It is not first time use
  } else {
    // It is first time
    await AsyncStorage.setItem("isFirstTime", 'true')
  }
}
///////////////  My code  //////////////

export default class App extends React.Component {
  constructor(props) {
    super(props);
    this.state = {
      azureLoginObject: {},
      loginSuccess: false,
    };
    this.azureInstance = new AzureInstance(credentials);
    this._onLoginSuccess = this._onLoginSuccess.bind(this);
  }

  _onLoginSuccess() {
    this.azureInstance
      .getUserInfo()
      .then((result) => {
        this.setState({
          loginSuccess: true,
          azureLoginObject: result,
        });
        console.log(result);
        //HERE EXAMPLE FOR STORE SOME VARIABLE INTO MY REDUX STORE
        store.dispatch(userPrincipalName(result.userPrincipalName));
        store.dispatch(givenName(result.mobilePhone));
      })
      .catch((err) => {
        console.log(err);
      });
  }

  render() {
    if (!this.state.loginSuccess) {
      return (
        <Provider store={store}>
          <AzureLoginView
            azureInstance={this.azureInstance}
            loadingMessage="Requesting access token"
            onSuccess={this._onLoginSuccess}
          />
        </Provider>
      );
    }

    const { userPrincipalName, givenName } = this.state.azureLoginObject;

    return (
      <Provider store={store}>
        <AppNavigator />
      </Provider>
    );
  }
}

【问题讨论】:

    标签: javascript reactjs react-native react-redux asyncstorage


    【解决方案1】:

    根据另一个答案,您将不得不使用 Asyn 存储来检查 Componentdidmount 上的值,如果不存在则设置该值。

    另外,其余的逻辑也必须稍作改动。 在这里,我们显示 ActivityIndi​​cator,直到检查完成,之后您可以做任何您想做的事情。 我无法测试此代码,因为它具有依赖项,但我已经放置了一些内联 cmets,以便您可以适当地进行更改。

    import { ActivityIndicator } from "react-native";
    export default class App extends React.Component {
        constructor(props) {
            super(props);
            this.state = {
                azureLoginObject: {},
                loginSuccess: false,
                loading: true
            };
    
        }
    
        async componentDidMount() {
            const firstTime = await AsyncStorage.getItem("isFirstTime")
            if (firstTime != null) {
                this.setState({ loading: false });
    
                //Place this based on your logic
                this.azureInstance = new AzureInstance(credentials);
                this._onLoginSuccess = this._onLoginSuccess.bind(this);
    
            } else {
                // It is first time
                await AsyncStorage.setItem("isFirstTime", 'true');
                this.setState({ loading: false })
            }
        }
    
        _onLoginSuccess() {
            this.azureInstance
                .getUserInfo()
                .then((result) => {
                    this.setState({
                        loginSuccess: true,
                        azureLoginObject: result,
                    });
                    console.log(result);
                    //HERE EXAMPLE FOR STORE SOME VARIABLE INTO MY REDUX STORE
                    store.dispatch(userPrincipalName(result.userPrincipalName));
                    store.dispatch(givenName(result.mobilePhone));
                })
                .catch((err) => {
                    console.log(err);
                });
        }
    
        render() {
            //Show activity indicator until async storage check is done
            if (this.state.loading)
                return <ActivityIndicator />;
    
            if (!this.state.loginSuccess) {
                return (
                    <Provider store={store}>
                        <AzureLoginView
                            azureInstance={this.azureInstance}
                            loadingMessage="Requesting access token"
                            onSuccess={this._onLoginSuccess}
                        />
                    </Provider>
                );
            }
    
            const { userPrincipalName, givenName } = this.state.azureLoginObject;
    
            return (
                <Provider store={store}>
                    <AppNavigator />
                </Provider>
            );
        }
    }
    

    【讨论】:

    • 你能告诉我所有的结合代码与 asyncstorage 吗?
    • 合并代码?我已经修改了你给检查的类
    • 我需要安装 npm install AsyncStorage 并导入 AsyncStorage 否?
    • 是的,你必须这样做
    • 2 问题:1. 为什么我们使用 asyncstorage 而不是 redux 来完成这个任务? 2. asyncstorage 保存的东西在哪里? (如果我想看到它,它的路径到底在哪里?)
    【解决方案2】:

    你可以使用asyncStorage:

    async componentDidMount() {
      const firstTime = await AsyncStorage.getItem("isFirstTime")
      if(firstTime != null) {
        // It is not first time use
      } else {
        // It is first time
        await AsyncStorage.setItem("isFirstTime", 'true')
      }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2022-06-25
      • 2021-10-22
      • 2021-12-21
      • 2011-06-08
      • 1970-01-01
      • 2014-12-22
      • 2011-08-15
      • 2019-09-25
      相关资源
      最近更新 更多