首页 > 解决方案 > 如何在输入类型日期中设置占位符 [React]

问题描述

我想知道如何在输入日期中设置占位符。

这是我的代码

<input type="text" onChange={(e) => { setbirth(moment(e.target.value).format('YYYYMMDD')) }} placeholder="생년월일" onFocus="(this.type = 'date')" onBlur="(this.type='text')" />

我已经查看了这个问题,但是使用此代码会引发错误。

onfocus="(this.type='date')" onblur="(this.type='text')"


Warning: Expected `onFocus` listener to be a function, instead got a value of `string` type.
Warning: Expected `onBlur` listener to be a function, instead got a value of `string` type.

有什么方法可以无误地更改它吗?

标签: javascripthtmlcssreactjs

解决方案


import React , { useRef } from "react";

const App= () => {
  const ref = useRef();
  return (
    <div>
      <input
        type="date"
        ref={ref}
        onChange={(e) => console.log(e.target.value)}
        onFocus={() => (ref.current.type = "date")}
        onBlur={() => (ref.current.type = "date")}
      />
    </div>
  );
}
export default  App

推荐阅读