I am building a CRUD app that stores name, dob, year as elements of an array (items) in state. Each displayed item from the array has an update and delete button. I'm currently stuck on the edit functionality and I want to:
- Set the defaultValue of each update inputbox to the displayed value
- Replace the existing value in the items array with the updated value
Here is the code I have so far
/* when 'edit' button is clicked */
<form onSubmit={this.handleUpdate}>
<input
className=""
name="name"
value={/* this section */}
placeholder= "Celebrant's Name"
ref={name => this.name = name}
required/>
<input
className=""
type="number"
name="day"
min="1"
max="31"
ref={day => this.day = day}
placeholder= "day"/>
<input
className=""
name="dob"
type="month"/>
<button type="submit">update</button>
<button onClick={this.handleEditCancel}>cancel</button>
</form>
/*displays each item in the items Array */
this.state.items.map((item, key) => (
<li key={key}>
<span> {item.name} </span>
<span> {item.day} </span>
<span> {item.dob} </span>
<button
className="btn btn-light"
onClick={this.handleEdit} >edit</button>
<button
className="btn btn-danger"
onClick={() => this.handleDelete(key)}>delete</button>
</li>
))}
/* edit functions */
handleEdit(){
this.setState({ toggle: true });
}
handleUpdate(event){
event.preventDefault();
console.log(this.name.value);
}
/* state */
this.state = {
name: '',
day: '',
dob: '',
toggle: false,
items : []
}
How can I implement this?