Write a react component that includes a form for users to fill out personal information…
- User
Write a react component that includes a form for users to fill out personal information and sends the form data to an express api. Include the following information in the form: Name, birthday, phone number, and email address. Use modern JS practices as of 2023 including fetch & async/await
Assistant
Here's a React component that includes a form for users to fill out their personal information and sends the form data to an Express API using modern JS practices as of 2023, including fetch and async/await:
import React, { useState } from 'react'; import { useNavigate } from 'react-router-dom'; const PersonalInfoForm = () => { const [name, setName] = useState(''); const [birthday, setBirthday] = useState(''); const [phoneNumber, setPhoneNumber] = useState(''); const [email, setEmail] = useState(''); const navigate = useNavigate(); const handleSubmit = async (e) => { e.preventDefault(); try { const response = await fetch('https://your-express-api-url.com/api/personal-info', { method: 'POST', headers: { 'Content-Type': 'application/json', }, body: JSON.stringify({ name, birthday, phoneNumber, email, }), }); if (response.ok) { const data = await response.json(); console.log('Form submitted successfully:', data); navigate('/success'); } else { console.error('Error submitting form:', response.status); } } catch (error) { console.error('Error submitting form:', error); } }; return ( <form onSubmit={handleSubmit}> <label htmlFor="name">Name:</label> <input type="text" id="name" value={name} onChange={(e) => setName(e.target.value)} required /> <label htmlFor="birthday">Birthday:</label> <input type="date" id="birthday" value={birthday} onChange={(e) => setBirthday(e.target.value)} required /> <label htmlFor="phoneNumber">Phone Number:</label> <input type="tel" id="phoneNumber" value={phoneNumber} onChange={(e) => setPhoneNumber(e.target.value)} pattern="[0-9]{3}-[0-9]{3}-[0-9]{4}" required /> <label htmlFor="email">Email Address:</label> <input type="email" id="email" value={email} onChange={(e) => setEmail(e.target.value)} required /> <button type="submit">Submit</button> </form> ); }; export default PersonalInfoForm;Replace
https://your-express-api-url.com/api/personal-infowith the actual URL of your Express API endpoint that handles the personal information form data. Also, make sure you have the necessary CORS configuration in place on the server-side to allow requests from the React frontend.
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.