Answer the question
In order to leave comments, you need to log in
React. Why does an element in a component become empty?
I am learning React.
import './Header.scss';
import React from 'react';
const menuBtn = document.querySelector('.btn');
console.log(menuBtn);
function Header() {
return (
<>
<div className="menu">
<div className="btn">
{/* ... */}
</div>
</>
);
}
export default Header;
const menuBtn = document.querySelector('.btn');
console.log(menuBtn);
Answer the question
In order to leave comments, you need to log in
Because the component is updated from top to bottom, i.e. when you update the component, the line first fires
const menuBtn = document.querySelector('.btn');
console.log(menuBtn);
function Header()
Why do you need to get an element through a selector? Right now your code doesn't make sense because Virtual DOM
In functional components, if you need access to the real home use the useRef hook
https://reactjs.org/docs/hooks-reference.html#useref
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question