最佳答案
我有一个简单的组件示例:
function App() {
const observed = useRef(null);
console.log(observed.current);
return (
<div ref={observed} className="App">
<h1>Hello CodeSandbox</h1>
<h2>Start editing to see some magic happen!</h2>
</div>
);
}
const rootElement = document.getElementById("root");
ReactDOM.render(<App />, rootElement);
我希望 observed.current
是类型元素,current 不是空的,而是包含所有属性的 div 元素。我的理解是:
但事实证明,.current
仍然是空的。这是不好的,因为我想要传递观察到的一个函数,期望一个元素类型的参数。