最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - This condition will always return 'false' since the types 'MutableRefObject<string |

programmeradmin1浏览0评论

I have a problem Using React and TS

I have state of the email and password in States

const emailRef = useRef<string | null>(null);
const passwordRef = useRef<string | null>(null);

Setting them like this:

const onEmailChange = (value: any) => {
        emailRef.current = value;
    };

    const onPasswordChange = (value: any) => {
        passwordRef.current = value;
    };

And try to use them onButtonEnter function butt get this issue:

const onButtonEnter = () => {
        if (!buttonDisabled) {
            setButtonDisabled(true);
            if (emailRef === '' && passwordRef === '') { - here I have an issue
                showAlert(errors.invalid);

!!! This condition will always return 'false' since the types 'MutableRefObject<string | null>' and 'string' have no overlap. TS2367

What it can be?

I have a problem Using React and TS

I have state of the email and password in States

const emailRef = useRef<string | null>(null);
const passwordRef = useRef<string | null>(null);

Setting them like this:

const onEmailChange = (value: any) => {
        emailRef.current = value;
    };

    const onPasswordChange = (value: any) => {
        passwordRef.current = value;
    };

And try to use them onButtonEnter function butt get this issue:

const onButtonEnter = () => {
        if (!buttonDisabled) {
            setButtonDisabled(true);
            if (emailRef === '' && passwordRef === '') { - here I have an issue
                showAlert(errors.invalid);

!!! This condition will always return 'false' since the types 'MutableRefObject<string | null>' and 'string' have no overlap. TS2367

What it can be?

Share Improve this question asked Jan 31, 2021 at 20:15 Yaroslav PlyahturYaroslav Plyahtur 971 gold badge2 silver badges7 bronze badges 1
  • 1 You are paring a reference to a DOM element with a string, which doesn't make sense. – Roberto Zvjerković Commented Jan 31, 2021 at 20:21
Add a ment  | 

1 Answer 1

Reset to default 3

The answer to your issue is in the error message:

  • emailRef is a react object - 'MutableRefObject<string | null>'
  • emailRef.current is your string

emailRef will never be '', emailRef.current on the other hand might very well be.

Change

if (emailRef === '' && passwordRef === '') {

into

if (emailRef.current === '' && passwordRef.current === '') {

And maybe, read up a little bit more on useRef in React docs.

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论