反应的on change输入延迟
我以前从未真正注意到过。不确定我的代码中是否有错误,或者Onchange始终以这种方式行为,但是我会得到输入延迟,并且不确定如何解决。这是我的意思的一个例子:
即使已正确输入用户名和密码,它也可以接收正确的状态。
我的代码相当简单:
const [state, setState] = useState<LoginStateType>(iLoginState)
const {username, password} = state
const handleChange = (event: ChangeEventType) => {
const {name, value} = event.target
setState({...state, [name]: value})
console.log(username)
}
{...}
<input type="text" name="username" value={username} onChange={handleChange}/>
<input type="text" name="password" value={password} onChange={handleChange}/>
任何帮助都将不胜感激,谢谢!
I've never really noticed this before; not sure if theres a bug in my code or if onChange always behaves this way, but I'm getting an input delay and am not sure how to resolve it. Heres an example of what I mean:
As you can see the console only prints the previous state which is a nuisance because that means to have the user log in they have to click the "Go" button twice for it to receive the correct state, even if the username and password has been correctly input.
My code is fairly straight forward:
const [state, setState] = useState<LoginStateType>(iLoginState)
const {username, password} = state
const handleChange = (event: ChangeEventType) => {
const {name, value} = event.target
setState({...state, [name]: value})
console.log(username)
}
{...}
<input type="text" name="username" value={username} onChange={handleChange}/>
<input type="text" name="password" value={password} onChange={handleChange}/>
Any help is appreciated, thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
因为状态更新可能是异步的 。
使用钩子,这可以通过
Since State Updates May Be Asynchronous.
With hooks, this can be achieved by
SetState是一个异步步骤,不要立即获得新值!
setState is a async step, not get new value immediately!
我最终做的可能有点麻烦,但完成了工作。除了国家(我已经设置的用户室内文本所必需的)之外,我还设置了两个Useref常数:
因此,我拥有的完整代码如下:
What I ended up doing was perhaps a bit cumbersome but it gets the job done. Aside from the state (which is necessary for the useContext I have set up) I also set up two useRef constants:
So the full code I have is as follows: