问题
I'm getting this error upon adding a form validation to my registration form component in React Native.
import React, { Component } from 'react';
import { View, AsyncStorage } from 'react-native';
import { Form, Item, Input, Label, Button, Text } from 'native-base';
import axios from 'axios';
import { JWT } from 'App';
class RegistrationForm extends Component {
constructor(props) {
super(props);
this.state = {
name: '',
email: '',
emailValidate: '',
pin: '',
pinValidate: '',
role: 'Consumer',
pin_confirmation: ''
};
}
onSubmit = () => {
const { name, email, pin, role, pin_confirmation } = this.state;
axios
.post('http://0.0.0.0:4000/api/v1/sign_up', {
user: {
name,
email,
pin,
role,
pin_confirmation
}
})
.then(response => {
AsyncStorage.setItem('JWT', response.data.jwt);
console.log(response.data.jwt);
this.props.navigation.navigate('Profile');
})
.catch(error => {
console.log(error);
});
};
validate(text, type) {
alph = /[a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*@(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?/g;
if (type == 'email') {
if (alph.test(text)) {
this.setState((emailValidate = true));
} else {
this.setState((emailValidate = false));
}
}
return type;
}
acceptTextInput = () => {
if (emailValidate == true) {
this.setState({ email: type });
}
};
// this.setState({someField:someValue})
render() {
return (
<View>
<Form>
<Item floatingLabel>
<Label>Name</Label>
<Input
value={this.state.name}
onChangeText={name => {
this.setState({ name });
}}
/>
</Item>
<Item floatingLabel last>
<Label>Email</Label>
<Input
autoCapitalize="none"
value={this.acceptTextInput}
onChangeText={text => this.validate(text, 'email')}
/>
</Item>
<Item floatingLabel last>
<Label>Pin</Label>
<Input
keyboardType={'number-pad'}
secureTextEntry
value={this.state.pin}
onChangeText={pin => {
this.setState({ pin });
}}
/>
</Item>
<Item floatingLabel last>
<Label>Pin Confirmation</Label>
<Input
keyboardType={'number-pad'}
secureTextEntry
value={this.state.pin_confirmation}
onChangeText={pin_confirmation => {
this.setState({ pin_confirmation });
}}
/>
</Item>
</Form>
<Button onPress={this.onSubmit}>
<Text> Submit</Text>
</Button>
</View>
);
}
}
export default RegistrationForm;
This is my registration form component. I call a validate function on onChangeText in the Email input. The validate function evaluates if the input in the Email form passes the regex expression. If yes, then the key 'emailValidate' in state is set to true. I then pass another function called acceptTextInput on the value of the Email input. The acceptTextInput evaluates if emailValidate is equal to true, and if it is, it sets the user input to the email state. For some reason, I'm getting this error & I really don't know why.
Hope i'm explaining this as clearly as I can.
来源:https://stackoverflow.com/questions/51821777/react-native-setstate-takes-an-object-of-state-variables-to-update-or-a-f