i just started with react, specifically i am trying to use forms, i tried to make a simple project, where i got a form component with some inputs, once i click the submit button it should pass the inputs value to the father component (app) and it should render it in a custom list component that i wrote.
The only problem is that when i try to access the event value it comes out as undefined, what am i doing wrong?
Following is my code, for simplicity sake i am trying to render just the first imput for now
import './App.css';
function Form({onChangeName}) {
return (
<div className="container">
<h1>
Hello
</h1>
<form onSubmit={onChangeName}>
<input name="fName" placeholder="First Name" />
<input name="lName" placeholder="Last Name" />
<input name="email" placeholder="Email" />
<button type="submit">Submit</button>
</form>
</div>
);
}
export default Form;
:
import List from './List';
import './App.css';
import Form from './Form';
import { useState } from 'react';
function App() {
const [contact, setContact] = useState({
fName: "",
lName: "",
email: ""
})
function onChangeName(event){
const { name, value } = event.target;
console.log(value)
setContact({
fname: value,
lName: "",
email: ""
})
}
return (
<div className="App">
<Form onChangeName={onChangeName} fName={contact.fName} lName={contact.lName} email={contact.email} />
<List fName={contact.fName} lName={contact.lName} email={contact.email}/>
</div>
);
}
export default App;
import logo from './logo.svg';
import './App.css';
function List({fName,lName,email}) {
return(
<ul>
<li>{fName}</li>
<li>{lName}</li>
<li>{email}</li>
</ul>
)
}
export default List;
3
Answers
You are listening on the submit event of the form, whose
event
object does not havename
andvalue
properties, like the change or input events of input elements would have.You can write an intermediate function in your child component to extract the data you need, and then call the handler you passed as a prop, like:
Then in the parent component:
I would also rename the
onChangeName
function to something more generic, since it affects the whole form.The event target is the form itself. So to get the input value directly like you are trying to do, replace the line
With the line
Sorry For the late Reply, this will help you seriously. (if you like my answer please mark it as correct answer for vote)
The issue you’re facing is due to how you’re handling the form submission and accessing the event value in your Form component. Here’s how you can modify your code to make it work as intended:
In your Form component, you should use the useState hook to manage
the form input values and handle changes to the input fields.
In the Form component, you can use the onChange event handler to
update the form state when input values change.
Modify the onChangeName function to prevent the default form
submission behavior and pass the current form state to the App
component.
Form.js
In the above code:
We use the useState hook to create a formData state object that stores the values of the form fields.
The handleInputChange function is called when any input field changes, and it updates the corresponding field in the formData state.
The handleSubmit function prevents the default form submission and calls the onSubmit prop function, passing the current formData.
Now, in your App component, you can update the onChangeName function and pass the form data to the List component:
App.js
List.js
With these modifications, your code should work as expected, and the input values from the Form component will be correctly passed to the List component for rendering.