我正在处理一个表单,该表单除其他字段外,还包含日历条目 - 日期和时间。代码如下:
{dates.map((d, i) => (
<div className="date-time-entry" key={i}>
<div className="date-picker">
<DatePicker
selected={new Date(d.startDate)}
dateFormat="dd MMM yyyy"
onChange={date => {
const newDates = [...dates]
newDates[i].startDate = date
setDates(newDates)
}}
/>
to
<DatePicker
selected={new Date(d.endDate)}
dateFormat="dd MMM yyyy"
onChange={date => {
const newDates = [...dates]
newDates[i].endDate = date
setDates(newDates)
}}
/>
{i ? (
<span
onClick={() => {
const newDates = dates.filter((d, k) => k !== i)
setDates(newDates)
}}
>
<FontAwesomeIcon icon={faTimes} />
</span>
) : null}
</div>
{dates[i].times.map((t, j) => {
return (
<div className="time-picker" key={j}>
at
<input
ref={(input) => { nameInput = input }}
value={t.start}
onChange={e => {
const newDates = [...dates]
newDates[i].times[j].start = e.target.value
setDates(newDates)
}}
/>{' '}
to{' '}
<input
ref={(input) => { nameInput = input }}
value={t.end}
onChange={e => {
const newDates = [...dates]
newDates[i].times[j].end = e.target.value
setDates(newDates)
}}
/>
<span
onClick={() => {
const newTimes = dates[i].times.filter((t, k) => k !== j)
const newDates = [...dates]
newDates[i].times = newTimes
setDates(newDates)
}}
>
<FontAwesomeIcon icon={faTimes} />
</span>
</div>
)
})}
<div
className="add-time"
onClick={() => {
const newDates = [...dates]
newDates[i].times = [
...newDates[i].times,
{ start: '12:00', end: '13:00' }
]
setDates(newDates)
}}
>
Add Time
</div>
</div>
)
)}
然后是useState()
钩子定义为:
const [dates, setDates] = useState([
{
startDate: new Date(),
endDate: new Date(),
times: [{ start: '12:00', end: '13:00' }]
}
])
我遇到的问题是,每当调用 onChange - 每次击键都会发生重新渲染并且我的组件失去焦点。
我尝试使用onBlur
anddefaultValue
而不是onChange
,但也存在焦点问题 - 需要两次单击才能切换焦点,因为第一次被重新渲染消耗。
我还尝试添加超时来处理onBlur
“丢失”的单击,如下所示:
<input
defaultValue={t.start}
onBlur={e => {
const value = e.target.value
timeout = setTimeout(() => {
const newDates = [...dates]
newDates[i].times[j].start = value
setDates(newDates)
}, 0)
}}
onFocus={() => {
clearTimeout(timeout)
}}
/>
最后一个版本不再失去焦点,而是失去状态更新。关于如何更好地处理这个问题,或者甚至修复我现有的代码的任何建议?