JavaScript wrangler.
Check out my books!
Web API Cookbook:
https://www.amazon.com/Web-API-Cookbook-JavaScript-Applications/dp/1098150694
Modern CSS:
https://www.amazon.com/dp/148426293X
You can also use them to store an element ref!
Using a ref is handy but as you probably know, changes to ref.current will not re-render the component.
What if you want it to?
You can use a function ref!
It looks like this:
const[popup,setPopup]=useState();
Then in your JSX:
<divref={setPopup}></div>
If you aren't familiar with this usage of a ref, a function ref gets called with the element. When the element is removed from the DOM, it is called again and passed null.
You can reference the DOM element now via the popup property (not popup.current as this isn't a "standard" ref.
You can also use them to store an element ref!
Using a ref is handy but as you probably know, changes to
ref.current
will not re-render the component.What if you want it to?
You can use a function ref!
It looks like this:
Then in your JSX:
If you aren't familiar with this usage of a ref, a function ref gets called with the element. When the element is removed from the DOM, it is called again and passed
null
.You can reference the DOM element now via the
popup
property (notpopup.current
as this isn't a "standard" ref.It can be useful!
Wow, nice trick! Thanks!