I want to send data of Contact Form[firstname,lastname,email,note] to a server but When I click Contact Button to Send data I got an e=Error Telling that firstname is not defined The mean that all the 5 variables not defined if Some Can Help This The Code of FormScreen.js:
JavaScript
x
149
149
1
import React, { useState } from 'react';
2
import {
3
View,
4
Text,
5
TextInput,
6
SafeAreaView,
7
Keyboard,
8
ScrollView,
9
Alert,
10
} from 'react-native';
11
12
import COLORS from '../src/conts/colors';
13
import Button from '../src/views/components/Button';
14
import Input from '../src/views/components/Input';
15
import Loader from '../src/views/components/Loader';
16
17
const ContactForm = ({navigation}) => {
18
const [inputs, setInputs] = React.useState({
19
firstname: '',
20
lastname: '',
21
email: '',
22
note: '',
23
});
24
const [errors, setErrors] = React.useState({});
25
const [loading, setLoading] = React.useState(false);
26
27
const validate = () => {
28
Keyboard.dismiss();
29
let isValid = true;
30
31
if (!inputs.firstname) {
32
handleError('Please input first name', 'firstname');
33
isValid = false;
34
}
35
36
if (!inputs.lastname) {
37
handleError('Please input last name', 'lastname');
38
isValid = false;
39
}
40
41
if (!inputs.email) {
42
handleError('Please input email', 'email');
43
isValid = false;
44
} else if (!inputs.email.match(/S+@S+.S+/)) {
45
handleError('Please input a valid email', 'email');
46
isValid = false;
47
}
48
if (!inputs.note) {
49
handleError('Please input note', 'note');
50
isValid = false;
51
}
52
53
if (isValid) {
54
submitData();
55
}
56
};
57
const submitData = ()=>{
58
fetch("https://flow.simpsimp.ai:2021/react/contact",{
59
method:"post",
60
headers:{
61
'Content-Type': 'application/json'
62
},
63
body:JSON.stringify({
64
65
firstname,
66
lastname,
67
email,
68
note
69
})
70
})
71
.then(res=>res.json())
72
.then(data=>{
73
alert(`${data.firstname} is saved successfuly`);
74
props.navigation.navigate("Home")
75
})
76
.catch(err=>{
77
alert("someting went wrong")
78
})
79
80
};
81
82
83
84
const handleOnchange = (text, input) => {
85
setInputs(prevState => ({prevState, [input]: text}));
86
};
87
const handleError = (error, input) => {
88
setErrors(prevState => ({prevState, [input]: error}));
89
};
90
return (
91
<SafeAreaView style={{backgroundColor: COLORS.white, flex: 1}}>
92
<Loader visible={loading} />
93
<ScrollView
94
contentContainerStyle={{paddingTop: 50, paddingHorizontal: 20}}>
95
<Text style={{color: COLORS.black, fontSize: 40, fontWeight: 'bold'}}>
96
Contact us
97
</Text>
98
<Text style={{color: COLORS.grey, fontSize: 18, marginVertical: 10}}>
99
Enter Your Details to Contact us
100
</Text>
101
<View style={{marginVertical: 20}}>
102
103
<Input
104
onChangeText={text => handleOnchange(text, 'firstname')}
105
onFocus={() => handleError(null, 'firstname')}
106
iconName="account-outline"
107
label="First Name"
108
placeholder="Enter your first name"
109
error={errors.firstname}
110
/>
111
112
<Input
113
onChangeText={text => handleOnchange(text, 'lastname')}
114
onFocus={() => handleError(null, 'lastname')}
115
iconName="account-outline"
116
label="Last Name"
117
placeholder="Enter your last name"
118
error={errors.lastname}
119
/>
120
121
<Input
122
onChangeText={text => handleOnchange(text, 'email')}
123
onFocus={() => handleError(null, 'email')}
124
iconName="email-outline"
125
label="Email"
126
placeholder="Enter your email address"
127
error={errors.email}
128
/>
129
130
<Input
131
onChangeText={text => handleOnchange(text, 'note')}
132
onFocus={() => handleError(null, 'note')}
133
iconName="note-outline"
134
label="Note"
135
placeholder="Enter your note"
136
error={errors.note}
137
/>
138
139
140
<Button title="Contact Us" onPress={validate} />
141
142
</View>
143
</ScrollView>
144
</SafeAreaView>
145
);
146
};
147
148
export default ContactForm;
149
This is The Error : enter image description here
Advertisement
Answer
It is because you never defined the variables in the body:
JavaScript
1
6
1
body: JSON.stringify({
2
firstname: inputs.firstname,
3
lastname: inputs.lastname,
4
email: inputs.email,
5
note: inputs.note,
6
}),
Use this part as the body