Prime React 日期未显示

huangapple go评论65阅读模式
英文:

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())

huangapple
  • 本文由 发表于 2023年2月7日 01:55:11
  • 转载请务必保留本文链接:https://go.coder-hub.com/75364890.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定