我想知道如何在输入日期中设置占位符。
这是我的代码
<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.
有什么办法可以不出错地更改它吗?
答案 0 :(得分:0)
错误是因为 onFocus 和 onBlur 是期望触发回调的事件发射器。在您的代码中,onFocus
和 onBlur
是一个语句而不是回调,因此是错误。
您可以使用 react RefObject
轻松完成。
参考:
import { useRef } from "react";
export default function App() {
const ref = useRef();
return (
<div>
<input
type="text"
ref={ref}
onChange={(e) => console.log(e.target.value)}
onFocus={() => (ref.current.type = "date")}
onBlur={() => (ref.current.type = "text")}
/>
</div>
);
}
答案 1 :(得分:0)
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
答案 2 :(得分:-1)
import { useRef } from "react";
export default function 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>
);
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/16.6.3/umd/react.production.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react-dom/16.6.3/umd/react-dom.production.min.js"></script>