React createRef current is null.

huangapple go评论105阅读模式
英文:

React createRef current is null

问题

以下是您提供的代码的翻译部分:

  1. import '../styles/Navbar.css';
  2. import * as React from 'react';
  3. import logo from '../assets/logo.webp';
  4. class Navbar extends React.Component {
  5. constructor(props) {
  6. super(props);
  7. this.ref1 = React.createRef();
  8. this.ref2 = React.createRef();
  9. };
  10. navAppear() {
  11. const navbar = this.ref1.current;
  12. const appearButton = this.ref2.current;
  13. this.switchDisplay(navbar, 'flex');
  14. this.switchClassBetween('bi-list', appearButton, 'bi-x');
  15. }
  16. switchClassBetween(className, element, className2) {
  17. if (element.classList.contains(className)) {
  18. element.classList.replace(className, className2);
  19. } else {
  20. element.classList.replace(className2, className);
  21. }
  22. }
  23. switchDisplay(element, displayType) {
  24. if (element.style.display === 'none') {
  25. element.style.display = displayType;
  26. } else {
  27. element.style.display = 'none';
  28. }
  29. }
  30. render() {
  31. const elems = [];
  32. const navHeaders = <div className="Navbar-headers Nav-separator" key="navbar-headers" ref={this.ref1}>
  33. <img className="Navbar-brand" src={logo} alt="Logo" />
  34. <a href="/#/" className="Navbar-brand-title">Adam Billard</a>
  35. </div>;
  36. elems.push(navHeaders);
  37. const navItems = <ul className="Navbar-items Nav-separator" key="navbar-items" ref={this.ref2}>
  38. <li className="Navbar-item"><a href="/#/" className="Navbar-link">Home</a></li>
  39. <li className="Navbar-item"><a href="/#/about" className="Navbar-link">About</a></li>
  40. <li className="Navbar-item"><a href="/#/contact" className="Navbar-link">Contact</a></li>
  41. </ul>;
  42. elems.push(navItems);
  43. const btn = <button className="Navbar-appear-button" onClick={this.navAppear()}><i className="bi bi-list"></i></button>;
  44. elems.push(btn);
  45. return <nav>{elems}</nav>;
  46. };
  47. }
  48. export default Navbar;

请注意,上面的翻译是您提供的代码部分,不包括错误信息和问题描述。如果您有任何其他问题或需要进一步的帮助,请随时提出。

英文:

Here I got this little code:

  1. import &#39;../styles/Navbar.css&#39;;
  2. import * as React from &#39;react&#39;;
  3. import logo from &#39;../assets/logo.webp&#39;;
  4. class Navbar extends React.Component {
  5. constructor(props) {
  6. super(props);
  7. this.ref1 = React.createRef();
  8. this.ref2 = React.createRef();
  9. };
  10. navAppear() {
  11. const navbar = this.ref1.current;
  12. const appearButton = this.ref2.current;
  13. this.switchDisplay(navbar, &#39;flex&#39;);
  14. this.switchClassBetween(&#39;bi-list&#39;, appearButton, &#39;bi-x&#39;);
  15. }
  16. switchClassBetween(className, element, className2) {
  17. if (element.classList.contains(className)) {
  18. element.classList.replace(className, className2);
  19. } else {
  20. element.classList.replace(className2, className);
  21. }
  22. }
  23. switchDisplay(element, displayType) {
  24. if (element.style.display === &#39;none&#39;) {
  25. element.style.display = displayType;
  26. } else {
  27. element.style.display = &#39;none&#39;;
  28. }
  29. }
  30. render() {
  31. const elems = [];
  32. const navHeaders = &lt;div className=&quot;Navbar-headers Nav-separator&quot; key=&quot;navbar-headers&quot; ref={this.ref1}&gt;
  33. &lt;img className=&quot;Navbar-brand&quot; src={logo} alt=&quot;Logo&quot; /&gt;
  34. &lt;a href=&quot;/#/&quot; className=&quot;Navbar-brand-title&quot;&gt;Adam Billard&lt;/a&gt;
  35. &lt;/div&gt;;
  36. elems.push(navHeaders);
  37. const navItems = &lt;ul className=&quot;Navbar-items Nav-separator&quot; key=&quot;navbar-items&quot; ref={this.ref2}&gt;
  38. &lt;li className=&quot;Navbar-item&quot;&gt;&lt;a href=&quot;/#/&quot; className=&quot;Navbar-link&quot;&gt;Home&lt;/a&gt;&lt;/li&gt;
  39. &lt;li className=&quot;Navbar-item&quot;&gt;&lt;a href=&quot;/#/about&quot; className=&quot;Navbar-link&quot;&gt;About&lt;/a&gt;&lt;/li&gt;
  40. &lt;li className=&quot;Navbar-item&quot;&gt;&lt;a href=&quot;/#/contact&quot; className=&quot;Navbar-link&quot;&gt;Contact&lt;/a&gt;&lt;/li&gt;
  41. &lt;/ul&gt;;
  42. elems.push(navItems);
  43. const btn = &lt;button className=&quot;Navbar-appear-button&quot; onClick={this.navAppear()}&gt;&lt;i className=&quot;bi bi-list&quot;&gt;&lt;/i&gt;&lt;/button&gt;;
  44. elems.push(btn);
  45. return &lt;nav&gt;{elems}&lt;/nav&gt;;
  46. };
  47. }
  48. export default Navbar;

So this code makes a Navbar that is supposed to be responsive, lemme explain.
The navbar is rendered, the Navbar-appear-button is on display:none, but if the screen width is less than 768, then the dislay of the button ```
Uncaught TypeError: Cannot read properties of null (reading 'style')
at Navbar.switchDisplay (Navbar.js:30:1)
at Navbar.navAppear (Navbar.js:17:1)
at Navbar.render (Navbar.js:51:1)
at finishClassComponent (react-dom.development.js:19752:1)
at updateClassComponent (react-dom.development.js:19698:1)
at beginWork (react-dom.development.js:21611:1)
at HTMLUnknownElement.callCallback (react-dom.development.js:4164:1)
at Object.invokeGuardedCallbackDev (react-dom.development.js:4213:1)
at invokeGuardedCallback (react-dom.development.js:4277:1)
at beginWork$1 (react-dom.development.js:27451:1)

  1. I&#39;m kinda new at React so idk what to do.
  2. And here&#39;s the error I&#39;m getting:
  3. And if I log my this.ref1:
  4. [this.ref1 logging on console](https://i.stack.imgur.com/OHEH2.png)
  5. So I don&#39;t know how to make the ref&#39;s current property equal to an HTMLElement or anything that will help me using style and classList properties.
  6. </details>
  7. # 答案1
  8. **得分**: 0
  9. - 不要在渲染时直接调用 `navAppear` 函数;而是将该函数传递给 `onClick`
  10. - 你还需要将 `this` 上下文绑定到该函数上。
  11. ```jsx
  12. onClick={this.navAppear.bind(this)}
英文:
  • Don't call the navAppear function directly during rendering; pass the function to onClick instead.
  • You need to bind the this context to the function as well.
  1. onClick={this.navAppear.bind(this)}

huangapple
  • 本文由 发表于 2023年3月4日 03:19:48
  • 转载请务必保留本文链接:https://go.coder-hub.com/75631087.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定