注册无效,注册未定义错误 React Native

Posted

技术标签:

【中文标题】注册无效,注册未定义错误 React Native【英文标题】:Signup is not function, signup is undefined error React Native 【发布时间】:2020-02-04 17:27:47 【问题描述】:

我刚刚从 Udemy 购买了 React Native 课程,Stephen Grider 是该课程的作者。所以基本上我在他打字的时候和他一起输入我的代码。但不幸的是,我在跟踪应用程序的注册过程中遇到了错误。每当我在输入电子邮件和密码后单击“注册”按钮时,我的屏幕就会显示此错误。请检查我的代码,如果有任何错误,请告诉我。

错误

注册不是一个函数。 (在注册时(电子邮件:电子邮件,密码: 密码 )', 'signup' 未定义);

错误截图

SignupScreen.js

import React,  useState, useContext  from 'react';
import  View, StyleSheet  from 'react-native';
import  Text, Button, Input  from 'react-native-elements';
import Spacer from '../components/Spacer';
import  Context as AuthContext  from '../context/AuthContext';

const SignupScreen = ( navigation ) => 
    const  state, signup  = useContext(AuthContext);
    const [email, setEmail] = useState('');
    const [password, setPassword] = useState('');

    return (
        <View style=styles.container>
            <Text h3 style=styles.heading>Sign Up for Tracker</Text>
            <Input
                label='Email'
                value=email
                onChangeText=setEmail
                autoCapitalize="none"
                autoCorrect=false
            />
            <Spacer />
            <Input
                secureTextEntry
                label='Password'
                value=password
                onChangeText=setPassword
                autoCapitalize="none"
                autoCorrect=false
            />
            <Spacer />
            <Spacer>
                <Button
                    title="Sign Up"
                    onPress=() => signup( email, password )
                />
            </Spacer>
        </View>
    );
;

SignupScreen.navigationOptions = () => 
    return 
        header: null
    ;
;
export default SignupScreen;

AutoContext.js

import createDataContext from "./createDataContext";
import trackerApi from '../api/tracker';

const authReducer = (state, action) => 
    switch (action.type) 
        default:
            return state;
    
;

const signup = dispatch => 
    return async ( email, password ) => 
        try 
            const response = await trackerApi.post('/signup',  email, password );
            console.log(response.data);
         catch (err) 
            console.log(err.message);
        
    ;
;
const signin = dispatch => 
    return ( email, password ) => 

    ;
;
const signout = dispatch => 
    return ( email, password ) => 

    ;
;

export const  Provider, Context  = createDataContext(
    authReducer,
     signin, signout, signup ,
     isSignedIn: false 
);

createDataContext.js

import React,  useReducer  from 'react';

export default (reducer, actions, defaultValue) => 
    const Context = React.createContext();

    const Provider = ( children ) => 
        const [state, dispatch] = useReducer(reducer, defaultValue);
        const boundActions = ;
        for (let key in boundActions) 
            boundActions[key] = actions[key](dispatch);
        

        return (
            <Context.Provider value= state, ...boundActions >
                children
            </Context.Provider>
        );
    ;
    return  Context, Provider ;
;

App.js

import React from 'react';
import  createAppContainer, createSwitchNavigator  from 'react-navigation';
import  createStackNavigator  from 'react-navigation-stack';
import  createBottomTabNavigator  from 'react-navigation-tabs';
import SignupScreen from './src/screens/SignupScreen';
import SigninScreen from './src/screens/SigninScreen';
import TrackListScreen from './src/screens/TrackListScreen';
import TrackDetailScreen from './src/screens/TrackDetailScreen';
import TrackCreateScreen from './src/screens/TrackCreateScreen';
import AccountScreen from './src/screens/AccountScreen';
import  Provider as AuthProvider  from './src/context/AuthContext';

const switchNavigator = createSwitchNavigator(
  loginFlow: createStackNavigator(
    Signup: SignupScreen,
    Signin: SigninScreen
  ),
  mainFlow: createBottomTabNavigator(
    trackListFlow: createStackNavigator(
      TrackList: TrackListScreen,
      TrackDetail: TrackDetailScreen
    ),
    TrackCreate: TrackCreateScreen,
    Account: AccountScreen
  )
);

const App = createAppContainer(switchNavigator);

export default () => 
  return (
    <AuthProvider>
      <App />
    </AuthProvider>
  );

【问题讨论】:

您将上下文内容定义为来自AuthContext 文件,但您的文件名为AutoContext 【参考方案1】:

我在createDataContext 文件中犯了一个错误。

改变这个条件

for (let key in boundActions) 
            boundActions[key] = actions[key](dispatch);
        

  for (let key in actions) 
            boundActions[key] = actions[key](dispatch);
        

【讨论】:

以上是关于注册无效,注册未定义错误 React Native的主要内容,如果未能解决你的问题,请参考以下文章

(react-native-svg 错误)“试图注册两个同名的视图 RNSVGRect”

React Native - iOS - 无效的`Podfile`文件:nil:NilClass的未定义方法`[]'

类型无效:需要一个字符串(对于内置组件)或一个类/函数(对于复合组件),但得到:在 react-native 中未定义

使用 Apollo 和 React-native 的身份验证问题

React-Native 高仿“掘金”App 注册和登录界面

React-Native 高仿“掘金”App 注册和登录界面