[英]How to use document.getElementById() with styled components in react?
I want to use JS functions like document.getElementById()
in my react project.我想在我的反应项目中使用像document.getElementById()
这样的 JS 函数。 I am new to styled components and don't know how to use these functions with them.我是样式组件的新手,不知道如何将这些功能与它们一起使用。 Do not refer to the official 'styled-components' page, I have already seen it.不要参考官方的“样式组件”页面,我已经看过了。
You can use useRef
react Hook for this.您可以useRef
使用useRef
react Hook。
const reqElement = React.useRef(null);
Now, you can add ref={reqElement}
in your required element.现在,您可以在所需元素中添加ref={reqElement}
。
After that you can use reqElement.current
to do those things that you can do via document.getElementById()
in javaScript.之后你可以使用reqElement.current
来做那些你可以通过 javaScript 中的document.getElementById()
做的事情。
I have finally found an answer to my question here.我终于在这里找到了我的问题的答案。 I laying down the gathered information in the following points.我将收集到的信息分为以下几点。
Here is a link to a Parallax effect video https://youtu.be/Q5y6pwoE3cM hope this can help you.这是一个视差效果视频的链接https://youtu.be/Q5y6pwoE3cM希望这可以帮助你。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.