简体   繁体   English

使用Expo通过React Native登录Google+

[英]Google+ Sign In with React Native, using Expo

I'm trying to implement a Sign In to Google from a React-native app. 我正在尝试从React-native应用程序实现Google登录。 I found this question where someone give a way to do it, but is not working anymore or I'm doing something wrong. 我发现这个问题有人在哪里提供了解决方法,但现在不起作用了,或者我做错了什么。

My code is this one: 我的代码是这样的:

App.js App.js

"use strict";
import React from 'react';
import { StyleSheet, Text, View } from 'react-native';
import { LoginGoogle } from './src/components/Login.js';

export default class App extends React.Component {
  render() {
    return (
      <View style={styles.container}>
        <Text>Open uuuup App.js ehh to start working on your app!</Text>
        <LoginGoogle/>
      </View>
    );
  }
}

const styles = StyleSheet.create({
  container: {
    flex: 1,
    backgroundColor: '#fff',
    alignItems: 'center',
    justifyContent: 'center',
  },
});

Login.js Login.js

"use strict";
import React, { Component } from 'react';
import Expo from 'expo';
import Button from 'react-native';
import { Text, View } from 'react-native';

class LoginGoogle extends Component {    

  signInWithGoogleAsync = async () => {
    try {
      const result = await Expo.Google.logInAsync({
        androidClientId: process.env.GOOGLE_ANDROID_CLIENT_ID,
        iosClientId: process.env.GOOGLE_IOS_CLIENT_ID,
        scopes: ['profile'],
      })

      if (result.type === 'success') {
        return result
      }
      return { cancelled: true }
    } catch (e) {
      return { error: e }
    }
  }


  onLoginPress = async () => {
    const result = await this.signInWithGoogleAsync()
    // if there is no result.error or result.cancelled, the user is logged in
    // do something with the result
  }

    render() {
      return (<Button onPress={this.onLoginPress}>Login</Button>)
    }
}

export default LoginGoogle;

When this is compiled, it says the following error: 编译时,它显示以下错误:

Uncaught Error: Invariant Violation: Element type is invalid: expected a string (for built-in components) or a class/function but got: undefinded. 未捕获的错误:始终违反:元素类型无效:预期为字符串(对于内置组件)或类/函数,但得到:未定义。 You likely forgot to export your component from the file it's defined in, or you might have mixed up default and named imports 您可能忘记了从定义文件中导出组件,或者您可能混淆了默认导入和命名导入

I already found when this error generally happen as states this other question . 我已经发现这个错误通常是在发生其他问题时指出的。 I already try importing LoginGoogle with and without curly braces, but without luck. 我已经尝试使用和不使用花括号来导入LoginGoogle,但是没有运气。

Any idea on what could be happening here? 对这里可能发生的事情有任何想法吗? I know that if I put only a Text in LoginGoogle.js and import it from App.js works like a charm, so probably it has something to do with the functions that are related to Google Sign In, but it's a guess, I could be wrong. 我知道,如果我只在LoginGoogle.js中放入一个文本,然后从App.js导入它,就像一个超级按钮一样,那么它可能与与Google登录相关的功能有关,但是我可以猜测是错的。

You only need to change this line: 您只需要更改此行:

import { LoginGoogle } from './src/components/Login.js';

to

import LoginGoogle from './src/components/Login.js';

The first line would work if you exported Login like this: 如果您按以下方式导出登录名,则第一行将起作用:

export {LoginGoogle}

and not 并不是

export default LoginGoogle;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM