Javascript 此条件将始终返回';假';由于类型';可变对象<;字符串| null>';和';字符串';没有重叠。TS2367
我有个问题 使用React和TS 我有美国的电子邮件状态和密码Javascript 此条件将始终返回';假';由于类型';可变对象<;字符串| null>';和';字符串';没有重叠。TS2367,javascript,reactjs,typescript,Javascript,Reactjs,Typescript,我有个问题 使用React和TS 我有美国的电子邮件状态和密码 const emailRef = useRef<string | null>(null); const passwordRef = useRef<string | null>(null); 并尝试在ButtonEnter函数上使用它们,以解决此问题: const onButtonEnter = () => { if (!buttonDisabled) { set
const emailRef = useRef<string | null>(null);
const passwordRef = useRef<string | null>(null);
并尝试在ButtonEnter函数上使用它们,以解决此问题:
const onButtonEnter = () => {
if (!buttonDisabled) {
setButtonDisabled(true);
if (emailRef === '' && passwordRef === '') { - here I have an issue
showAlert(errors.invalid);
!!!此条件将始终返回“false”,因为类型“mutableReObject”和“string”没有重叠。TS2367
可能是什么?您的问题的答案在错误消息中:
是一个react对象-“emailRef
”MutableRefObject
是您的字符串emailRef.current
emailRef
将永远不会是'
,emailRef。另一方面,当前的
很可能是
改变
if (emailRef === '' && passwordRef === '') {
进入
也许,请在React docs中阅读更多关于
useRef
的内容。您将对DOM元素的引用与字符串进行比较,这毫无意义。
if (emailRef === '' && passwordRef === '') {
if (emailRef.current === '' && passwordRef.current === '') {