I have a quick question because I am a bit confused with JSX in functional components.
I am doing a small project to explore more complex uses of useState() and in the final portion of this challenge, you have to render the Contacts you type into an input form.
This is my code:
import React, { useState } from "react";
function App() {
const [inputData, setInputData] = useState({ firstName: "", lastName: "" });
const [contactsData, setContactsData] = useState([]);
function handleChange(event) {
const { name, value } = event.target;
setInputData((prevData) => {
return {
...prevData,
// will copy over all properties from the previous input data and then replace whichever one we are currently working on with the updated value
[name]: value,
};
});
}
function handleSubmit(event) {
event.preventDefault();
setContactsData((prevContacts) => [...prevContacts, inputData]);
}
const contacts = contactsData.map((contact) => (
<h2 key={contact.firstName + contact.lastName}>
{contact.firstName} {contact.lastName}
</h2>
));
return (
<>
<form onSubmit={handleSubmit}>
<input
placeholder="First Name"
name="firstName"
value={inputData.firstName}
onChange={handleChange}
/>
<input
placeholder="Last Name"
name="lastName"
value={inputData.lastName}
onChange={handleChange}
/>
<br />
<button>Add contact</button>
</form>
{contacts}
</>
);
}
export default App;
My questions is why does const contacts have JSX outside of the return? I thought all JSX had to be within the return?
When I try to input the contacts without an outside const variable, I get a reference Error?
Thanks!