reactjs - 我怎样才能让这个函数接受一个数组元素的 id,然后调度一个动作?MERN 堆栈
问题描述
该组件的目的是呈现一个 div,其中包含在今天之后发生的每个约会的详细信息。然后,用户应该能够在输入字段中填写详细信息,然后按提交。提交后,我正在尝试发送一个 redux 操作,我需要它获取单击提交按钮的特定约会 div 的 id。
我似乎无法让 handleSubmit 函数正常工作。我不知道我是否将约会._id 错误地传递到按钮的 onClick 处理程序中,或者我是否将它错误地带入了 handleSubmit 函数,或者完全是其他的东西。请注意,有两个 _id,一个用于约会数组中的特定约会,一个来自用户的 _id,从其父组件传递到组件。
有谁知道如何让它正常工作?
import React, { useState } from 'react'
import { useDispatch } from 'react-redux'
import { confirmAppointment } from '../../../actions/appointment'
const ConfirmAppointment = ({user}) => {
const dispatch = useDispatch()
const { appointments, _id } = user?.result
const today = new Date()
const [reasonForMassage, setReasonForMassage] = useState('')
const formData = {
reasonForMassage
}
// SET UP AN ONCLICK FOR EACH BUTTON TO SUBMIT IT'S OWN DISPATCH TO UPDATE APPOINTMENT
const handleSubmit = (e, appointmentId) => {
e.preventDefault()
console.log(appointmentId, reasonForMassage)
//update appointment with the appointment id
dispatch(confirmAppointment(_id, appointmentId, formData))
}
return (
appointments?.length !== 0 ? (
<div style={{marginTop: '3em'}}>
<h4>Upcoming Appointments</h4>
{appointments && appointments?.map((appointment) => (
new Date(appointment?.date) >= today ? (
<div style={{marginBottom: '3em'}} key={appointment._id} >
<table className="ui table">
<thead>
<tr>
<th>Date</th>
<th>Time</th>
<th>Duration</th>
</tr>
</thead>
<tbody>
<tr>
<td>{appointment?.date}</td>
<td>{appointment?.time}</td>
<td>{appointment?.duration}</td>
</tr>
</tbody>
</table>
<form className="ui form" >
<div className="ui fields">
<div className="ui field">
<label>Reason for booking massage:</label>
<input type="text" value={reasonForMassage} onChange={(e)=>setReasonForMassage(e.target.value)}/>
</div>
<div className="ui field">
<h5>I give consent to massage the following areas:</h5>
<div>
<input type="checkbox" />
<label>Glutes</label>
<input type="checkbox" />
<label>Abdomen</label>
<input type="checkbox" />
<label>Chest</label>
<input type="checkbox" />
<label>Inner thighs</label>
</div>
</div>
<div className="ui field">
<label>Are there any other areas you would not like to be massaged?</label>
<input type="text" />
</div>
</div>
<button onClick={()=>handleSubmit(appointment?._id)} className="ui button">Confirm Appointment</button>
</form>
</div>
) : (
<div></div>
)
))}
</div>
) : (
<div>
No upcoming appointments
</div>
)
)
}
export default ConfirmAppointment
解决方案
您需要更改按钮的 onClick 功能
<button
onClick={() => handleSubmit(appointment?._id)}
className='ui button'
>
Confirm Appointment
</button>
至
<button
onClick={e => handleSubmit(e, appointment?._id)}
className='ui button'
>
Confirm Appointment
</button>
推荐阅读
- python - SQLite 更新来自 TextFields Python Kivy 的多个值
- amazon-web-services - Terraform 和 ElasticBeanstalk。当 EC2 由 elasticbeanstalk 启动时,如何在 EC2 中安装 EFS?
- python - 将新包添加到 Conda 环境
- python - 共享 Jupyter 细胞
- wordpress - Nginx:将多个 WordPress 站点作为子目录托管返回 404 页
- c++ - 你能确定 x 在 for() 循环中递增的时间吗?(初学者)
- html - 有没有办法使用 Flask 动态添加下拉菜单
- c - 为什么这两个代码给出不同的输出?不是 for 循环只是 while 循环的简写吗?
- ajax - Giphy uplaod 适用于 .ajax 但不能获取
- javascript - DiscordJS v12 - 我怎么能有 args.slice .join(" ") 变量