英文:
Prime React Date not showing
问题
我正在使用Prime React组件库。我尝试使用日历组件 https://primereact.org/calendar/ 但无法弄清如何设置日历的默认值(初始值)。我尝试创建一个硬编码的状态,但它只会在我选择日历上的日期时显示,如果我将Calendar组件的实际value属性设置为"02/01/2023",它甚至不会显示。我甚至尝试了不同的格式。非常感谢任何帮助。
英文:
I am using prime reacts component library. I'm trying to use the calendar component https://primereact.org/calendar/ but can not figure out how to set the default value (initial value) of the calendar. I tried creating a hardcoded state but it just wont display, it will only display when I pick a date on the calendar. It wont even display if I make the actual value prop in the Calendar "02/01/2023". I even tried different formats. Any help is greatly appreciated.
const EventForm = (props) => {
const [start, setStart] = useState('02/01/2023')
return (
<>
<div className="field">
<span className="p-float-label">
<Calendar
name="start"
value={start}
onChange={(e) => handleChange(e)}
/>
<label>Start</label>
</span>
</div>
</>
)}
export default EventForm
答案1
得分: 0
你必须使用一个 Date
对象,而不是一个字符串。
const [start, setStart] = useState(new Date())
英文:
You have to use a Date
object not a String.
const [start, setStart] = useState(new Date())
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论