在React中,ref引用虚拟DOM还是实际DOM? [英] In React do ref's reference the virtual DOM, or the actual DOM?

查看:324
本文介绍了在React中,ref引用虚拟DOM还是实际DOM?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我假设是虚拟DOM,而React会通过差异处理它.但是我有一个招聘人员说,ref会影响实际的DOM,我不知道这是怎么回事.我认为他们只是误会了.

I'm assuming the virtual DOM, and that React takes care of it with diff'ing. But I had a recruiter say that ref's affect the actual DOM, I can't see how this can be. I assume that they were just mistaken.

推荐答案

引用应引用实际的DOM.引用的一种用法是与第三方DOM库集成,因此您可以使用引用直接修改DOM.如果Refs引用了虚拟DOM,我认为需求无法满足.您修改了虚拟DOM,但不能确保所做的修改将与实际DOM同步.此外,如果要在使用react时修改实际的DOM,则应始终修改组件的状态,而不要通过Refs来修改DOM.

Refs should reference the actual DOM. One usage of Refs is integrating with third-party DOM libraries, so you can directly modify the DOM using Refs. If Refs reference the virtual DOM, I don't think the demand can be meet. You modify a virtual DOM, but you can't make sure the modification would be synchronized to the actual DOM. Besides, if you want to modify actual DOM when using react, you should always modify the state of components instead of modifying the DOM by Refs.

这篇关于在React中,ref引用虚拟DOM还是实际DOM?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆