简体   繁体   English

this.setState 真的会让组件重新渲染吗?

[英]Does this.setState really make component re-render?

something about this.setState in class components is confusing me. class 组件中有关 this.setState 的某些内容让我感到困惑。 in react docs, i read that this.setState may be asynchronous in react docs.在反应文档中,我读到 this.setState 在反应文档中可能是异步的。 im okay with this and i tested it with logging the updated value after this.setState.我对此没问题,我通过在 this.setState 之后记录更新的值来测试它。 but it is really confusing me: when we call this.setState, react calls render method of the class to re-render the ui.但这真的让我很困惑:当我们调用 this.setState 时,react 会调用 class 的渲染方法来重新渲染 ui。 but this.setState is asynchronous and it means that first render method will be called and ui will be re-rendered by react, then the value in the state will be changed?但是this.setState是异步的,这意味着将调用第一个render方法并通过react重新渲染ui,那么state中的值会改变吗? so how does component show us the updated value.那么组件如何向我们显示更新的值。 i dont..?我不..? am i thinking right.我想对了吗。 thanks for helping.谢谢你的帮助。

this.setState is asynchronous and it means that first render method will be called and ui will be re-rendered by react, then the value in the state will be changed? this.setState 是异步的,也就是说第一个render方法会被react调用,ui会被react重新渲染,那么state中的值会改变吗? so how does component show us the updated value.那么组件如何向我们显示更新的值。 i dont..?我不..? am i thinking right?我想对了吗?

I think you missed something.setState is asynchronous which means that the actions are batched together for performance gains.我认为您错过了某些东西。setState 是异步的,这意味着将这些操作分批在一起以提高性能。 But why?但为什么? since it's not aa WebAPI or server call?因为它不是 WebAPI 或服务器调用? This is because setState alters the state and causes rerendering.这是因为 setState 改变了 state 并导致重新渲染。 This can be an expensive operation and making it synchronous might leave the browser to behave unexpectedly.这可能是一项昂贵的操作,并且使其同步可能会使浏览器出现意外行为。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM