[英]How to set placeholder in input type date [React]
I want to know how to set placeholder in input date.我想知道如何在输入日期中设置占位符。
this is my code这是我的代码
<input type="text" onChange={(e) => { setbirth(moment(e.target.value).format('YYYYMMDD')) }} placeholder="생년월일" onFocus="(this.type = 'date')" onBlur="(this.type='text')" />
I've looked through the question, but using this code throws an error.我已经浏览了这个问题,但使用此代码会引发错误。
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.
Is there any way to change it without error?有什么办法可以不出错地更改它吗?
The error is because onFocus and onBlur are event emitters that expect to fire a callback.错误是因为 onFocus 和 onBlur 是期望触发回调的事件发射器。 In your code,
onFocus
and onBlur
is a statement instead of a callback and thus the error.在您的代码中,
onFocus
和onBlur
是一个语句而不是回调,因此是错误。
You can use react RefObject
to do it easily.您可以使用 react
RefObject
轻松完成。 Reference:参考:
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>
);
}
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
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>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.