对于普通DOM元素,findDOMNode vs getElementById [英] findDOMNode vs getElementById for plain DOM elements

查看:102
本文介绍了对于普通DOM元素,findDOMNode vs getElementById的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不太确定这个问题有一个真正的答案,但我想知道在React应用程序中使用

I'm not quite sure there's a real answer to this question but I was wondering if it's better to find regular DOM elements in a React app by using

a找到常规DOM元素是否更好。 refs和ReactDOM.findDOMNode

a. refs and ReactDOM.findDOMNode

b。 plain old document.getElementById

b. plain old document.getElementById

推荐答案

我更喜欢refs的模式,因为它会鼓励你不要为元素添加ID,因此允许您在单个页面上使用react组件的多个实例。 react中的ref特性将为您提供由该组件的特定实例呈现的元素,而不是来自其任何重复渲染的元素。

I would prefer the pattern of refs because it would encourage you not to add IDs to elements and therefore allow you to use multiple instances of a react component on a single page. The ref feature in react will get you the element rendered by that specific instance of the component, and not an element from any duplicate renderings of it.

这篇关于对于普通DOM元素,findDOMNode vs getElementById的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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