Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/453.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/3/reactjs/27.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
Javascript 使用setState方法的类的状态何时发生更改?_Javascript_Reactjs - Fatal编程技术网

Javascript 使用setState方法的类的状态何时发生更改?

Javascript 使用setState方法的类的状态何时发生更改?,javascript,reactjs,Javascript,Reactjs,我有一个表单,在onChange事件上调用hanldeChange函数。函数定义为: handleChange = (e) =>{ this.setState({ [e.target.name]:e.target.value }) console.log(`Password:${this.state.password} and ConfirmPassword:${this.state.confirmPassword}`) if(this.st

我有一个表单,在
onChange
事件上调用hanldeChange函数。函数定义为:

handleChange = (e) =>{
    this.setState({
        [e.target.name]:e.target.value
    })
    console.log(`Password:${this.state.password} and ConfirmPassword:${this.state.confirmPassword}`)
    if(this.state.password !== this.state.confirmPassword){
        this.setState({
            passwordError:"Passwords don't match."
        })
    }
    else if(this.state.password === this.state.confirmPassword){
        this.setState({
            passwordError:""
        })
    }
我的问题是,在调用setState之后,状态是否会立即更改-“是”,这是我的想法,但如果Password和confirmPassword都相同,则上述代码不起作用


这里出了什么问题?

对在单个事件范围内执行的所有
setState
语句进行批处理。因此,在您的情况下,整个函数都将被执行,只有到那时react才会真正更新状态

这篇关于React 18的文章很好地解释了这一点:
对在单个事件范围内执行的所有
setState
语句进行批处理。因此,在您的情况下,整个函数都将被执行,只有到那时react才会真正更新状态

这篇关于React 18的文章很好地解释了这一点:

您将在
setState
的回调中获得更新的状态。因此,您的
handleChange
应该是

handleChange = (e) =>{
    this.setState({
        [e.target.name]:e.target.value
    }, () => {
        if(this.state.password !== this.state.confirmPassword){
            this.setState({
                passwordError:"Passwords don't match."
            })
        }
        else if(this.state.password === this.state.confirmPassword){
            this.setState({
                passwordError:""
            })
        }
    })
    
}

有关更多信息,请访问您将在
设置状态的回调中获得更新状态。因此,您的
handleChange
应该是

handleChange = (e) =>{
    this.setState({
        [e.target.name]:e.target.value
    }, () => {
        if(this.state.password !== this.state.confirmPassword){
            this.setState({
                passwordError:"Passwords don't match."
            })
        }
        else if(this.state.password === this.state.confirmPassword){
            this.setState({
                passwordError:""
            })
        }
    })
    
}
欲了解更多信息,请访问