I am trying to save the input given by the user using the input tag, to an array in the state. My App.js looks like this
class App extends Component {
state = {
generalInfo: [
{
email : "",
name: "",
contactNumber: "",
}
],
}
handleGeneralSubmit = (e) => {
e.preventDefault();
console.log("general submit clicked"); //this function should contain code to take input and store.
}
render() {
return (
<div>
<GeneralInfo onGeneralSubmit={this.handleGeneralSubmit} onGeneralEdit={this.handleGeneralEdit} />
</div>
);
}
}
export default App;
--------------------
another file generalinfo.jsx
class GeneralInfo extends Component {
render() {
const { onGeneralSubmit, onGeneralEdit } = this.props;
return (
<div>
<form>
<div className="form-group m-4">
<label htmlFor="InputEmail1">Email address</label>
<input
type="email"
className="form-control"
id="InputEmail1"
aria-describedby="emailHelp"
placeholder="Enter email"
/>
<small id="emailHelp" className="form-text text-muted">
We'll never share your email with anyone else.
</small>
</div>
<div className="form-group m-4">
<label htmlFor="inputAddress">Name :</label>
<input
type="text"
className="form-control"
id="inputName"
placeholder="Full Name"
/>
</div>
<div className="form-group m-4">
<label htmlFor="inputAddress">Phone Number :</label>
<input
type="text"
className="form-control"
id="inputContactNumber"
placeholder="Contact Number"
/>
</div>
<button
onClick={onGeneralSubmit}
type="submit"
className="btn btn-primary m-4"
>
Submit
</button>
<button
onClick={onGeneralEdit}
type="submit"
className="btn btn-warning m-4 "
//disabled={this.props.generalInfo.length === 0 ? "disabled" : " "}
>
Edit
</button>
</form>
</div>
);
}
}
export default GeneralInfo;
Can anyone help me with this, I am still new to handling states. And I am not getting what exactly should be code in the function. I tried to add e.target.value to the state, but it is showing error.