React Hooks are now available in React 16.8. There are 10 different hooks, you can read about them here. When I needed a ref to a dom element yesterday I reached for useRef.

const containerRef = useRef(null);

  

This isn’t a ref to anything unless you pass the ref to a tag.

return (<div ref={containerRef}></div>);

  

Now the ref will be assigned a dom element that you can use. In this example I’m using the useEffect hook to execute a side effect after the render takes place. Use the current attribute to access the current dom node.

useEffect(() => {
  containerRef.current.style = 'background-color: green;'
})

 

相关文章:

  • 2021-11-29
  • 2021-08-12
  • 2021-11-16
  • 2022-12-23
  • 2021-11-23
猜你喜欢
  • 2021-12-15
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-06-27
  • 2022-12-23
相关资源
相似解决方案