This is a copy of this post, but using a function component and hooks.