React-Native向用户传递电子邮件和密码以提交按钮

时间:2019-11-15 08:34:26

标签: firebase react-native

从'../MyModules/FirebaseAPI'导入FirebaseAPI;

function commit(){

import FirebaseAPI from '../MyModules/FirebaseAPI';

export default function LinksScreen() {
  const [email, onChangeText] = React.useState('Enter Email');
  const [password, onChangeText2] = React.useState('Enter Password');

  const submit = () => {
    FirebaseAPI.createUser(email, password)
  }
  return (
    <KeyboardAvoidingView style={styles.wrapper} behavior="padding">
          <View style={styles.scrollViewWrapper}>
            <ScrollView style={styles.scrollView}>
              <Text style={styles.loginHeader}>Creat an Account </Text>
              <TextInput 
                style={{ height: 40, borderColor: 'gray', borderWidth: 1 }}
                onChangeText={text => onChangeText(text)}
                value={email}
              />
              <TextInput 
                style={{ height: 40, borderColor: 'gray', borderWidth: 1 }}
                onChangeText={text => onChangeText2(text)}
                value={password}
              />
              <TouchableOpacity
                style={{marginTop: '5%'}}
                onPress= {submit()}>
                <View>
                    <Text>Submit</Text>
                </View>
                
                
                
//code from FirebaseAPI.js
import * as firebase from 'firebase'

export const createUser = (email, password) => {
    firebase.auth().createUserWithEmailAndPassword(email, password)
    
    .catch((error) => console.log('createUser error: ', error));
}

//等

我的错误是 TypeError:未定义不是对象(正在评估'_FirebaseAPI.default.createUser')

我认为这是一个范围界定问题,但不确定如何解决。还是很新的反应。任何帮助都会很棒!

1 个答案:

答案 0 :(得分:1)

emailpassword不是submit函数的范围。您要么需要将submit函数移到组件函数中,要么将值传递给该函数

export default function LinksScreen() {
  const [email, onChangeText] = React.useState('Enter Email');
  const [password, onChangeText2] = React.useState('Enter Password');

  const submit = () => {
    FirebaseAPI.createUser(email, password)
  } 
  return (
  ....
  )

OR

 <TouchableOpacity
    style={{marginTop: '5%'}}
    onPress= {() => submit(email, password)}>
      <View>
        <Text>Submit</Text>
      </View>
</TouchableOpacity>

还将FirebaseAPI导入导入为

import * as FirebaseAPI from '../MyModules/FirebaseAPI';