Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/reactjs/23.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/objective-c/26.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Reactjs 当比赛发生时做出反应_Reactjs_Jsx - Fatal编程技术网

Reactjs 当比赛发生时做出反应

Reactjs 当比赛发生时做出反应,reactjs,jsx,Reactjs,Jsx,我想停止一个循环,当它符合一个条件,但它只是没有打破我试着用抛出e和抛出新的typeerror捕捉循环,但它只是没有打破我如何让它打破这里是我想打破的代码 LoginAuth = (Res) => { try { if (Res.username === this.state.Username && Res.password === this.state.Password) { return (

我想停止一个循环,当它符合一个条件,但它只是没有打破我试着用抛出e和抛出新的typeerror捕捉循环,但它只是没有打破我如何让它打破这里是我想打破的代码

LoginAuth = (Res) => {
    try {
        if (Res.username === this.state.Username && Res.password === this.state.Password) {
            return (
                this.setState({
                    isLoggedIn: true,
                }, function () {

                }),
                this.hideModal()
            );
        } else {
            console.log("Gets to login auth and false");
        }
}
当用户在state.username和state.password中输入的值等于变量Res中的值时,我想让它停止循环,但我无法让它停止循环。我该如何做

这是调用此函数的函数

CheckLoginAuth() {
    console.log("gets to check login auth");
    console.log(this.state.AuthRes);
    console.log("Username=" + this.state.Username);
    console.log("Password=" + this.state.Password);
    var self = this;
    this.props.AuthRes.forEach(function (Res) {
        return (
            console.log("ID=" + Res.id),
            console.log("Username=" + Res.username),
            console.log("Password=" + Res.password),
            self.LoginAuth(Res)
        );
    });
}
编辑

错误为无法读取未定义的属性“状态”

LoginAuth = (Res) => {
    if (Res.username === this.state.Username && Res.password === this.state.Password) {
        return (
            this.setState({
                isLoggedIn: true,
            }, function () {
            }),
            this.hideModal()
        );
    } else {
        console.log("Gets to login auth and false");
    }
}
CheckLoginAuth() {
    console.log("gets to check login auth");
    console.log(this.state.AuthRes);
    console.log("Username=" + this.state.Username);
    console.log("Password=" + this.state.Password);
    var self = this;
    this.props.AuthRes.every(function (Res) {
        console.log("ID=" + Res.id);
        console.log("Username=" + Res.username);
        console.log("Password=" + Res.password);
        return (
            self.LoginAuth(Res)
        );
    });
}

循环执行,但在第一个循环中停止,不管它是真是假

无法停止执行
forEach()
。事实上,各国:

除了抛出异常之外,无法停止或中断
forEach()
循环。如果需要这种行为,那么
forEach()
方法是错误的工具。改为使用普通循环。如果正在测试数组元素的谓词,并且需要一个布尔返回值,则可以使用
each()
some()

因此,正如建议的那样,您可以使用

some()。如果找到这样的元素,
some()
立即返回
true
。否则,
some()
返回
false


因此,您的函数可以写成:

this.props.AuthRes.some(function (Res) {
    return (
        console.log("ID=" + Res.id),
        console.log("Username=" + Res.username),
        console.log("Password=" + Res.password),
        self.LoginAuth(Res)
    );
});
这将在回调返回true时停止迭代,或者直到您用完了要迭代的元素


编辑

要解决未定义的
错误的
无法读取属性“state”,最简单的方法是使用箭头函数:

this.props.AuthRes.some((Res) => {

无法停止执行
forEach()
。事实上,各国:

除了抛出异常之外,无法停止或中断
forEach()
循环。如果需要这种行为,那么
forEach()
方法是错误的工具。改为使用普通循环。如果正在测试数组元素的谓词,并且需要一个布尔返回值,则可以使用
each()
some()

因此,正如建议的那样,您可以使用

some()。如果找到这样的元素,
some()
立即返回
true
。否则,
some()
返回
false


因此,您的函数可以写成:

this.props.AuthRes.some(function (Res) {
    return (
        console.log("ID=" + Res.id),
        console.log("Username=" + Res.username),
        console.log("Password=" + Res.password),
        self.LoginAuth(Res)
    );
});
这将在回调返回true时停止迭代,或者直到您用完了要迭代的元素


编辑

要解决未定义的
错误的
无法读取属性“state”,最简单的方法是使用箭头函数:

this.props.AuthRes.some((Res) => {

我把它弄坏了,把它修好了

我的密码是

LoginAuth = (Res) => {
if (Res.username === this.state.Username && Res.password === this.state.Password) {
    return (
        this.setState({
            isLoggedIn: true,
        }, function () {
        }),
        this.hideModal()
    );
} else {
    console.log("Gets to login auth and false");
}
}
CheckLoginAuth() {
    console.log("gets to check login auth");
    console.log(this.state.AuthRes);
    console.log("Username=" + this.state.Username);
    console.log("Password=" + this.state.Password);
    var self = this;
    this.props.AuthRes.every(function (Res) {
        console.log("ID=" + Res.id);
        console.log("Username=" + Res.username);
        console.log("Password=" + Res.password);
        return (
            self.LoginAuth(Res)
        );
    });
}
现在是

LoginAuth = (Res) => {
    if (Res.username === this.state.Username && Res.password === this.state.Password) {
            this.setState({
                isLoggedIn: true,
            })

            this.hideModal();
            return true;
    } else {
        console.log("Gets to login auth and false");
        return null;
    }
}
CheckLoginAuth() {
    console.log("gets to check login auth");
    console.log(this.state.AuthRes);
    console.log("Username=" + this.state.Username);
    console.log("Password=" + this.state.Password);
    var self = this;
    this.props.AuthRes.some(function (Res) {
        console.log("ID=" + Res.id);
        console.log("Username=" + Res.username);
        console.log("Password=" + Res.password);
        return self.LoginAuth(Res);
    });
}

我没有返回真实的值或虚假的值

我通过使其中断来修复它

我的密码是

LoginAuth = (Res) => {
if (Res.username === this.state.Username && Res.password === this.state.Password) {
    return (
        this.setState({
            isLoggedIn: true,
        }, function () {
        }),
        this.hideModal()
    );
} else {
    console.log("Gets to login auth and false");
}
}
CheckLoginAuth() {
    console.log("gets to check login auth");
    console.log(this.state.AuthRes);
    console.log("Username=" + this.state.Username);
    console.log("Password=" + this.state.Password);
    var self = this;
    this.props.AuthRes.every(function (Res) {
        console.log("ID=" + Res.id);
        console.log("Username=" + Res.username);
        console.log("Password=" + Res.password);
        return (
            self.LoginAuth(Res)
        );
    });
}
现在是

LoginAuth = (Res) => {
    if (Res.username === this.state.Username && Res.password === this.state.Password) {
            this.setState({
                isLoggedIn: true,
            })

            this.hideModal();
            return true;
    } else {
        console.log("Gets to login auth and false");
        return null;
    }
}
CheckLoginAuth() {
    console.log("gets to check login auth");
    console.log(this.state.AuthRes);
    console.log("Username=" + this.state.Username);
    console.log("Password=" + this.state.Password);
    var self = this;
    this.props.AuthRes.some(function (Res) {
        console.log("ID=" + Res.id);
        console.log("Username=" + Res.username);
        console.log("Password=" + Res.password);
        return self.LoginAuth(Res);
    });
}

我没有返回真实值或虚假值

你在使用承诺吗?如果是,那么您需要在Promise.reject中返回异常。您在那里使用promises吗?如果是,那么您需要在Promise.reject中返回exception。我是否会将If语句放在checkLoginAuth中,因为如果您在list@andywilson,我不确定整个应用程序是如何工作的。因此,我不能确切地说某些代码应该如何以及应该放在哪里。我所能做的就是回答你的问题:一旦满足条件,如何打破循环。:)@如果这个答案对你有帮助,请考虑将它标记为“接受”和/或为可见性进行投票。@ CHIRS它一直保持循环,即使我已经放置了某个部分,现在它又给了我一个错误,我将在我的错误中写入错误。edit@andywilson,您需要确保在满足条件时返回真实值。我是否会将If语句放在checkLoginAuth中,因为如果您在list@andywilson,我不确定整个应用程序是如何工作的。因此,我不能确切地说某些代码应该如何以及应该放在哪里。我所能做的就是回答你的问题:一旦满足条件,如何打破循环。:)@如果这个答案对你有帮助,请考虑将它标记为“接受”和/或为可见性进行投票。@ CHIRS它一直保持循环,即使我已经放置了某个部分,现在它又给了我一个错误,我将在我的错误中写入错误。edit@andywilson,您需要确保在满足条件时返回真实值。