[英]react-datetime: How to set the time to now() from a button
我正在使用反應日期時間
我希望用戶使用按鈕Immediate
輕松地將時間設置為當前時間。 而不是手動選擇當前時間。
我正在嘗試以下
const [currentDateTime, setcurrentDateTime] = useState(null);
<Datetime
inputProps={{
placeholder: "MM-DD-YYYY HH:mm",
required: true,
}}
viewMode="time"
value={currentDateTime} <--- THE PROBLEM WITH THIS IS, IT DOES NOT SET THE SELECTED DATETIME.
/>
<Button color="primary" className="ml-1" onClick={() => setcurrentDateTime(new Date())}>
{"Immediate"}
</Button>
要使用Immediate
按鈕值,我正在使用value={currentDateTime}
。 但是這不允許我使用選定的值
這該怎么做。 當我按下Immediate
按鈕時有new Date()
值。 並在我選擇日期時間時具有selected
值
發生這種情況是因為您尚未將onChange
事件添加到您的Datetime
:
const [currentDateTime, setcurrentDateTime] = useState(null);
return (
<>
<Datetime
inputProps={{
placeholder: "MM-DD-YYYY HH:mm",
required: true
}}
viewMode="time"
value={currentDateTime}
onChange={setcurrentDateTime} // <--- You need to add onChange
/>
<Button
color="primary"
className="ml-1"
onClick={() => setcurrentDateTime(new Date())}
>
{"Immediate"}
</Button>
</>
);
查看此 CodeSandbox:
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.