【发布时间】:2019-09-02 00:28:40
【问题描述】:
不确定为什么这一行会导致问题。看来我正确使用了钩子。
导致错误的行是:
const { theme } = useUserContext();
在getStyle 函数中。如果我删除此行,则组件加载正常。有什么想法吗?
在我看来,我正在遵循挂钩规则。
import React, {useEffect, useRef, useState} from 'react';
import { Text, Button, TextInput, View, StyleSheet, Linking } from 'react-native';
import { Formik, FormikProps, ErrorMessage } from 'formik';
import * as Yup from 'yup';
import { WideButton } from '../../../components/Buttons';
import { HeaderText, ErrorText, LinkText } from '../../../components/StyledText';
import Colors from '../../../constants/Colors';
import { useUserContext } from "../../../context/UserContext";
import { RenderErrorMessage } from "../com/FormMessage";
const LoginForm = (props) => {
const { user, logon, logoff } = useUserContext();
const [loginErrorMessage, setLoginErrorMessage] = useState("");
const usernameRef = useRef();
const onLoginSubmit = (values, actions) => {
axios.post(config.aogApiUrl + '/users/authenticate', values)
.then(res => {
//console.log(res.data);
setLoginErrorMessage("");
logon(res.data);
setShowLoginModal(false);
props.navigation.goBack();
})
.catch(error =>{
let errorMsg = error.response ? error.response.data : error.message;
setLoginErrorMessage(errorMsg);
actions.setSubmitting(false);
});
}
useEffect(() => {
usernameRef.current.focus();
});
return (
<Formik
initialValues={{ username: 'jason', password: 'Yankees1'}}
validationSchema={LoginSchema}
onSubmit={(values, actions) => {
props.onSubmit(values, actions);
}}
>
{({ handleChange, handleBlur, handleSubmit, values, errors, touched, isSubmitting }) => (
<View style={getStyle('container')} >
<HeaderText>Log in to AOG</HeaderText>
<RenderErrorMessage errorMessage={loginErrorMessage} />
<TextInput
name="username"
autoComplete="username"
ref={usernameRef}
style={getStyle('textInput')}
placeholder="Username"
onChangeText={handleChange('username')}
//onBlur={handleBlur('username')}
value={values.username} />
<ErrorMessage name="username">{msg => <ErrorText>{msg}</ErrorText>}</ErrorMessage>
<View style={{marginVertical: 5}} />
<TextInput
name="password"
autoComplete="current-password"
style={getStyle('textInput')}
placeholder="Password"
onChangeText={handleChange('password')}
//onBlur={handleBlur('password')}
value={values.password}
secureTextEntry={true}
onSubmitEditing={handleSubmit} />
<ErrorMessage name="password">{ msg => <ErrorText>{msg}</ErrorText> }</ErrorMessage>
<View style={{marginVertical: 5}} />
<View style={getStyle('submitContainer')}>
<WideButton
onPress={handleSubmit}
title="Submit"
label="Click to submit" />
</View>
<View style={getStyle('linkContainer')}>
<LinkText>Forgot password?</LinkText>
<LinkText>Forgot username?</LinkText>
<LinkText>Need to Register?</LinkText>
</View>
</View>
)}
</Formik>
);
}
const LoginSchema = Yup.object().shape({
username: Yup.string()
.min(3, 'Too Short!')
.max(15, 'Too Long!')
.required('Username is required!'),
password: Yup.string()
.min(6, 'Must be at least 6 characters')
.required('Password is required!'),
});
const getStyle = (style) => {
const { theme } = useUserContext();
const styles = {
container:{
margin: 20,
},
submitContainer: {
marginTop: 12,
marginBottom: 12
},
linkContainer:{
flex:1,
flexDirection: 'row',
alignItems: 'flex-start',
marginTop: 12,
justifyContent: 'space-between'
},
textInput:{
height: 40,
width: 'auto',
borderColor: 'gray',
backgroundColor: '#E3B39C',
borderWidth: 1,
padding: 15,
borderRadius: 20,
fontSize: 18,
margin: 5
}
}
return StyleSheet.flatten(styles[style]);
};
export default LoginForm;
【问题讨论】:
-
你能把你看到的错误输出控制台分享给我吗?
标签: reactjs