Im trying to create a basic subscription component where an user just fills an email and subscribes to a service. So far i cant input my data. Trying to get used with React with Hooks and Typescript, just the last one is getting in my way :D I Handle Input:
const handleEmail = (e: React.ChangeEvent<HTMLInputElement>): void => {
const { name, value } = e.target;
setData({...data, [name]: value})
}
I have the post function
const postEmail = (e: React.ChangeEvent<HTMLInputElement>): void => {
e.preventDefault()
let obj = {
email: data.email
};
setLoading(true)
setSubmitted(false);
postGeneralSub.postGeneralSub(obj).then(response => {
setData({
email: response.data.email
})
setLoading(false)
setSubmitted(true);
console.log(response.data);
}).catch(e => {
console.log(e);
});
(e want to send "email":"blablabla")
And the form
<div>
<GeneralCountryForm
label="Insert Email"
onChange={handleEmail}
value={data.email}
submit={postEmail}
/>
<Button>Submit data</Button>
</div>
With the child component GeneralCountryForm:
type GeneralSubProps = {
label: string;
onChange: any;
value: string;
submit: any
}
export const GeneralCountryForm: FunctionComponent<GeneralSubProps> = ({ label, onChange, value, submit }) =>
<form onSubmit={submit}>
<label>{label}</label>
<input type="text" value={value} onChange={onChange} />
</form>
Any tip on what im doing wrong and how i can improve this logic / error handling ?