React Native 中 document.getElementById
的替代方案是什么?我想访问 DOM 中的元素。我正在使用功能组件。
最佳答案
您可以使用refs
。假设您有一个 div,并且您想要获取 DOM 引用。
const MyFuncComp = React.memo(() => {
const divRef = React.useRef(null)
/* your other logic here */
/* refer the div node using divRef.current - its the HTML DOM Node.
return (
<div ref={divRef.current}>
// whatever you want to render
</div>
)
})
资源:https://reactjs.org/docs/refs-and-the-dom.html
https://stackoverflow.com/questions/63783036/
相关文章:
reactjs - CRA typescript环境导出类型时如何解决解析错误
flutter - 如何在 Flutter 中绘制尖三角形边?
javascript - select2 on select data-select2-id 属性添
python - 为什么在 Python 3.x 中的循环中使用 list() 会返回内存错误?
react-native - 如何在没有 Firebase 的情况下为 React Native A
post - ESP32 异步 Web 服务器 POST 方法不起作用
javascript - 使用逗号自动格式化数字时如何保留预期的光标位置?
python - 使用 tokenizer.encode_plus 时遇到问题