经验首页 前端设计 程序设计 Java相关 移动开发 数据库/运维 软件/图像 大数据/云计算 其他经验
当前位置:技术经验 » JS/JS库/框架 » React » 查看文章
React报错解决之ref返回undefined或null
来源:jb51  时间:2022/8/3 12:49:06  对本文有异议

总览

当我们试图在其对应的DOM元素被渲染之前访问其current属性时,React的ref通常会返回undefined或者null。为了解决该问题,可以在useEffect钩子中访问ref,或者当事件触发时再访问ref

  1. import {useRef, useEffect} from 'react';
  2.  
  3. export default function App() {
  4. const ref = useRef();
  5.  
  6. console.log(ref.current); // ??? undefined here
  7.  
  8. useEffect(() => {
  9. const el2 = ref.current;
  10. console.log(el2); // ??? element here
  11. }, []);
  12.  
  13. return (
  14. <div>
  15. <div ref={ref}>
  16. <h2>Hello</h2>
  17. </div>
  18. </div>
  19. );
  20. }

useEffect

useRef()钩子可以传递一个初始值作为参数。该钩子返回一个可变的ref对象,ref对象上的current属性被初始化为传递的参数。

我们没有为useRef传递初始值,因此其current属性设置为undefined。如果我们将null传递给钩子,如果立即访问其current属性,将会得到null

需要注意的是,我们必须访问ref对象上的current属性,以此来访问设置了ref属性的div元素。

当我们为元素传递ref属性时,比如说,<div ref={myRef} /> ,React将ref对象的.current属性设置为相应的DOM节点。

我们使用useEffect钩子,是因为我们想要确保ref已经设置在元素上,并且元素已经渲染到DOM上。

如果我们尝试在组件中直接访问ref上的current属性,我们会得到undefined,是因为 ref 还没有被设置,而且 div 元素还没有被渲染。

事件

你也可以在事件处理函数中访问refcurrent属性。

  1. import {useRef, useEffect} from 'react';
  2.  
  3. export default function App() {
  4. const ref = useRef();
  5.  
  6. console.log(ref.current); // ??? undefined here
  7.  
  8. useEffect(() => {
  9. const el2 = ref.current;
  10. console.log(el2); // ??? element here
  11. }, []);
  12.  
  13. const handleClick = () => {
  14. console.log(ref.current); // ??? element here
  15. };
  16.  
  17. return (
  18. <div>
  19. <div ref={ref}>
  20. <h2>Hello</h2>
  21. </div>
  22.  
  23. <button onClick={handleClick}>Click</button>
  24. </div>
  25. );
  26. }

当用户点击按钮的时候,ref已经被设置好了,相应的元素已经被渲染到DOM中,所以我们能够访问它。

总结

可以在useEffect钩子中访问ref,或者当事件触发时再访问ref。也就是说,要确保元素已经渲染到DOM上。

到此这篇关于React报错解决之ref返回undefined或null的文章就介绍到这了,更多相关React ref返回undefined null内容请搜索w3xue以前的文章或继续浏览下面的相关文章希望大家以后多多支持w3xue!

附:ref的值根据节点的类型而有所不同

  • 当在refHTML元素上使用该属性时,ref在构造函数中创建的属性将React.createRef()接收底层DOM元素作为其current属性。
  • 在ref自定义类组件上使用该属性时,该ref对象将接收组件的已安装实例作为其current。

您可能无法ref在函数组件上使用该属性,因为它们没有实例。

  1. class CustomTextInput extends React.Component {
  2. constructor(props) {
  3. super(props);
  4. // create a ref to store the textInput DOM element
  5. this.textInput = React.createRef();
  6. this.focusTextInput = this.focusTextInput.bind(this);
  7. }
  8.  
  9. focusTextInput() {
  10. // Explicitly focus the text input using the raw DOM API
  11. // Note: we're accessing "current" to get the DOM node
  12. this.textInput.current.focus();
  13. }
  14.  
  15. render() {
  16. // tell React that we want to associate the <input> ref
  17. // with the `textInput` that we created in the constructor
  18. return (
  19. <div>
  20. <input
  21. type="text"
  22. ref={this.textInput} />
  23.  
  24. <input
  25. type="button"
  26. value="Focus the text input"
  27. onClick={this.focusTextInput}
  28. />
  29. </div>
  30. );
  31. }
  32. }

current当组件安装时,React将为该属性分配DOM元素,并null在卸载时将其分配回。ref更新发生之前componentDidMount或componentDidUpdate生命周期方法。

原文链接:bobbyhadz.com/blog/react-…

作者:Borislav Hadzhiev

 友情链接:直通硅谷  点职佳  北美留学生论坛

本站QQ群:前端 618073944 | Java 606181507 | Python 626812652 | C/C++ 612253063 | 微信 634508462 | 苹果 692586424 | C#/.net 182808419 | PHP 305140648 | 运维 608723728

W3xue 的所有内容仅供测试,对任何法律问题及风险不承担任何责任。通过使用本站内容随之而来的风险与本站无关。
关于我们  |  意见建议  |  捐助我们  |  报错有奖  |  广告合作、友情链接(目前9元/月)请联系QQ:27243702 沸活量
皖ICP备17017327号-2 皖公网安备34020702000426号