React createRef current is null.

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

React createRef current is null

问题

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

    import '../styles/Navbar.css';
    import * as React from 'react';
    import logo from '../assets/logo.webp';
    
    class Navbar extends React.Component {
        constructor(props) {
            super(props);
            this.ref1 = React.createRef();
            this.ref2 = React.createRef();
        };
    
        navAppear() {
            const navbar = this.ref1.current;
            const appearButton = this.ref2.current;
    
            this.switchDisplay(navbar, 'flex');
            this.switchClassBetween('bi-list', appearButton, 'bi-x');
        }
    
        switchClassBetween(className, element, className2) {
            if (element.classList.contains(className)) {
                element.classList.replace(className, className2);
            } else {
                element.classList.replace(className2, className);
            }
        }
    
        switchDisplay(element, displayType) {
            if (element.style.display === 'none') {
                element.style.display = displayType;
            } else {
                element.style.display = 'none';
            }
        }
    
        render() {
            const elems = [];
            const navHeaders = <div className="Navbar-headers Nav-separator" key="navbar-headers" ref={this.ref1}>
                <img className="Navbar-brand" src={logo} alt="Logo" />
                <a href="/#/" className="Navbar-brand-title">Adam Billard</a>
            </div>;
            elems.push(navHeaders);
            const navItems = <ul className="Navbar-items Nav-separator" key="navbar-items" ref={this.ref2}>
                <li className="Navbar-item"><a href="/#/" className="Navbar-link">Home</a></li>
                <li className="Navbar-item"><a href="/#/about" className="Navbar-link">About</a></li>
                <li className="Navbar-item"><a href="/#/contact" className="Navbar-link">Contact</a></li>
            </ul>;
            elems.push(navItems);
    
            const btn = <button className="Navbar-appear-button" onClick={this.navAppear()}><i className="bi bi-list"></i></button>;
            elems.push(btn);

            return <nav>{elems}</nav>;
        };
    }
    
    export default Navbar;

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

英文:

Here I got this little code:

    import &#39;../styles/Navbar.css&#39;;
    import * as React from &#39;react&#39;;
    import logo from &#39;../assets/logo.webp&#39;;
    
    class Navbar extends React.Component {
        constructor(props) {
            super(props);
            this.ref1 = React.createRef();
            this.ref2 = React.createRef();
        };
    
        navAppear() {
            const navbar = this.ref1.current;
            const appearButton = this.ref2.current;
    
            this.switchDisplay(navbar, &#39;flex&#39;);
            this.switchClassBetween(&#39;bi-list&#39;, appearButton, &#39;bi-x&#39;);
        }
    
        switchClassBetween(className, element, className2) {
            if (element.classList.contains(className)) {
                element.classList.replace(className, className2);
            } else {
                element.classList.replace(className2, className);
            }
        }
    
        switchDisplay(element, displayType) {
            if (element.style.display === &#39;none&#39;) {
                element.style.display = displayType;
            } else {
                element.style.display = &#39;none&#39;;
            }
        }
    
        render() {
            const elems = [];
            const navHeaders = &lt;div className=&quot;Navbar-headers Nav-separator&quot; key=&quot;navbar-headers&quot; ref={this.ref1}&gt;
                &lt;img className=&quot;Navbar-brand&quot; src={logo} alt=&quot;Logo&quot; /&gt;
                &lt;a href=&quot;/#/&quot; className=&quot;Navbar-brand-title&quot;&gt;Adam Billard&lt;/a&gt;
            &lt;/div&gt;;
            elems.push(navHeaders);
            const navItems = &lt;ul className=&quot;Navbar-items Nav-separator&quot; key=&quot;navbar-items&quot; ref={this.ref2}&gt;
                &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;
                &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;
                &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;
            &lt;/ul&gt;;
            elems.push(navItems);
    
            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;;
            elems.push(btn);

            return &lt;nav&gt;{elems}&lt;/nav&gt;;
        };
    }
    
    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)


I&#39;m kinda new at React so idk what to do.
And here&#39;s the error I&#39;m getting:
And if I log my this.ref1:
[this.ref1 logging on console](https://i.stack.imgur.com/OHEH2.png)
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.
</details>
# 答案1
**得分**: 0
- 不要在渲染时直接调用 `navAppear` 函数;而是将该函数传递给 `onClick`。
- 你还需要将 `this` 上下文绑定到该函数上。
```jsx
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.
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:

确定