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

I have a problemUsing React and TSI have state of the email and password in Statesconst emailRef = u

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.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744786936a4593684.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信