我正在练习React native。我创建了两个输入字段。用户可以在其中键入电子邮件和密码。我用三个变量设定了一个状态,它们是电子邮件,密码和errorMessage。对于电子邮件验证,我使用了Firebase。当提交表单而没有填写任何内容时,我得到了invalid error
,因此这意味着我的firebase auth
可以正常工作。但是,当用电子邮件和密码字段填写表格时,我得到了错误Error: signInWithEmailAndPassword failed: First argument "email" must be a valid string.
当我控制台登录handleChange Events
时。我收到一堆东西。看起来像这样:
am event SyntheticEvent {
"_dispatchInstances": FiberNode {
"tag": 5,
"key": null,
"type": "RCTSinglelineTextInputView",
},
"_dispatchListeners": [Function _onChange],
"_targetInst": FiberNode {
"tag": 5,
"key": null,
"type": "RCTSinglelineTextInputView",
},
"bubbles": undefined, and so on ........
我使用了onChangeText
而不是React native的onChange
。我也尝试使用onChangeText
,但遇到了同样的错误。有什么办法可以在功能组件中提交本机表单?
这是我的登录表单
import React, { ReactElement, useState } from 'react';
import styled from 'styled-components';
import { Text, View, TextInput, TouchableOpacity } from 'react-native';
import * as firebase from 'firebase';
interface Props {
}
export default function Login({ }: Props): ReactElement {
const [state, setState] = useState({
email: '',
password: '',
errorMessage: null
})
const { errorMessage } = state;
const handleChange = event => {
console.log("I am event", event);
setState({
...state,
[event.target.id]: event.target.value
});
};
const handleSubmit = event => {
event.preventDefault();
const { email, password } = state;
firebase.auth().signInWithEmailAndPassword(email, password).catch(error => setState({ errorMessage: error.message }))
};
// const handleSubmit = () => {
// const { email, password } = state;
// firebase.auth().signInWithEmailAndPassword(email, password).catch(error => setState({ errorMessage: error.message }))
// }
return (
<Container>
<Greeting >{`Hello again. \nWelcome Back`}</Greeting>
<ErrorMessageContainer >
{ErrorMessage && <ErrorMessage >{errorMessage} </ErrorMessage>}
</ErrorMessageContainer>
<Form style={{ marginHorizontal: 30 }}>
<View>
<InputTitle>
Email Address
</InputTitle>
<Input
autoCapitalize="none"
onChange={handleChange}
value={state.email}
id="email"
></Input>
</View>
<View style={{ marginTop: 32 }}>
<InputTitle>
password
</InputTitle>
<Input
secureTextEntry
autoCapitalize="none"
onChange={handleChange}
value={state.password}
id="password"
></Input>
</View>
</Form>
<SubmitButton style={{ marginHorizontal: 30 }}
onPress={handleSubmit}
>
<ButtonText>
Sign In
</ButtonText>
</SubmitButton>
<TouchableOpacity style={{ marginTop: 30, alignItems: "center" }} >
<ButtonText style={{ color: "black", fontSize: 15 }}>
New to Social App?
<Text style={{ fontWeight: "500", color: "#E9446A" }}> Sign up </Text>
</ButtonText>
</TouchableOpacity>
</Container>
)
};
const Container = styled.View`
flex:1;
`
const Greeting = styled.Text`
margin-top: 30px;
font-weight: 400;
text-align: center;
font-size: 20px
`
const ErrorMessageContainer = styled.View`
height: 72px;
align-items: center;
justify-content: center;
margin-top: 30px;
`
const ErrorMessage = styled.Text`
font-weight: 500;
color: red;
font-size: 15px;
`
const Form = styled.View`
margin-bottom: 48px;
`;
const InputTitle = styled.Text`
color: #8a8f9e;
font-size: 10px;
text-transform: uppercase;
`
const Input = styled.TextInput`
border-bottom-color: #8a8f9e;
border-bottom-width: 0.5px;
height: 40px;
font-size: 15px;
color: black;
`;
const SubmitButton = styled.TouchableOpacity`
background-color: #00203FFF;
height: 52px;
border-radius: 4px;
align-items: center;
justify-content: center;
`;
const ButtonText = styled.Text`
color: #fff;
font-weight: 500;
`
答案 0 :(得分:0)
我建议您使用react-native库中的TextInput
:
https://reactnative.dev/docs/handling-text-input
执行以下操作:
const handleChange = (label, value) => {
setstate({
...state,
[label]: value
})
}
<TextInput
autoCapitalize="none"
onChangeText={text => handleChange("email",text)}
value={email}
id="email"
>
</TextInput>
我希望这会有所帮助,并且不要忘记仔细检查每个组件的导入以避免错误。