{"id":1815,"date":"2021-05-24T16:10:52","date_gmt":"2021-05-24T16:10:52","guid":{"rendered":"https:\/\/lvboard.infostore.in.ua\/?p=1815"},"modified":"2021-05-24T16:10:52","modified_gmt":"2021-05-24T16:10:52","slug":"5-awesome-react-hooks","status":"publish","type":"post","link":"https:\/\/lvboard.infostore.in.ua\/?p=1815","title":{"rendered":"5 Awesome React Hooks"},"content":{"rendered":"\n<p>The React community has created incredible hooks since this feature was released, I will show some of the 5 that I use and help me a lot.<\/p>\n\n\n\n<!--more-->\n\n\n\n<ul><li><a href=\"https:\/\/dev.to\/tuliocalil\/5-awesome-react-hooks-23jc#image-lazy-load\">Image lazy load<\/a><\/li><li><a href=\"https:\/\/dev.to\/tuliocalil\/5-awesome-react-hooks-23jc#outside-click-hook\">Outside click hook<\/a><\/li><li><a href=\"https:\/\/dev.to\/tuliocalil\/5-awesome-react-hooks-23jc#get-browser-location\">Get browser location<\/a><\/li><li><a href=\"https:\/\/dev.to\/tuliocalil\/5-awesome-react-hooks-23jc#read-from-and-write-to-clipboard\">Read from and Write to clipboard<\/a><\/li><li><a href=\"https:\/\/dev.to\/tuliocalil\/5-awesome-react-hooks-23jc#set-document-title\">Set document title<\/a><\/li><\/ul>\n\n\n\n<h6>generated with&nbsp;<a href=\"https:\/\/summaryze-forem.vercel.app\/\">Summaryze Forem ?<\/a><\/h6>\n\n\n\n<h2>Image lazy load ?<\/h2>\n\n\n\n<p><a href=\"https:\/\/github.com\/robcalcroft\/react-use-lazy-load-image\">React use lazy load image<\/a>&nbsp;uses the&nbsp;<a href=\"https:\/\/developer.mozilla.org\/en-US\/docs\/Web\/API\/Intersection_Observer_API\">Intersection Observer API<\/a>&nbsp;to provide a performant solution to lazy loading images, it a super lightweight so won&#8217;t add any extra bulk to your app, and its very simple to use:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>import React from 'react';\nimport useLazyLoadImage from 'react-use-lazy-load-image';\n\nfunction App() {\n  useLazyLoadImageReact();\n\n  return (\n    &lt;div&gt;Lots of content that means the image is off screen goes here&lt;\/div&gt;\n    &lt;img src=\"IMAGE DATA\" data-img-src=\"https:\/\/link-to-my-image.com\/image.png\" alt=\"My image\" \/&gt;\n\n  )\n}\n<\/code><\/pre>\n\n\n\n<h2>Outside click hook ?<\/h2>\n\n\n\n<p>I think that&nbsp;<a href=\"https:\/\/github.com\/Andarist\/use-onclickoutside\/\">useOnClickOutside<\/a>&nbsp;is one of the bests, with this hook, you can easily capture outside clicks from an element, very useful for a modal for example.<br>Here is a demo code:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>import * as React from 'react'\nimport useOnClickOutside from 'use-onclickoutside'\n\nexport default function Modal({ close }) {\n  const ref = React.useRef(null)\n  useOnClickOutside(ref, close)\n\n  return &lt;div ref={ref}&gt;{'Modal content'}&lt;\/div&gt;\n}\n<\/code><\/pre>\n\n\n\n<h2>Get browser location ?<\/h2>\n\n\n\n<p><a href=\"https:\/\/bit.dev\/giladshoham\/react-hooks\/use\/use-location\">useLocation<\/a>&nbsp;help you to get browser location.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>import {useLocation} from 'react-use';\n\nconst Demo = () =&gt; {\n  const state = useLocation();\n\n  return (\n    &lt;pre&gt;\n      {JSON.stringify(state, null, 2)}\n    &lt;\/pre&gt;\n  );\n};\n<\/code><\/pre>\n\n\n\n<h2>Read from and Write to clipboard \u2328\ufe0f<\/h2>\n\n\n\n<p><a href=\"https:\/\/www.npmjs.com\/package\/use-clippy\">useClippy<\/a>&nbsp;is a small hook that helps you to easily read from and write to the user&#8217;s clipboard board:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>import useClippy from 'use-clippy';\n\nexport default function MyComponent() {\n\n  const &#91;clipboard, setClipboard] = useClippy();\n\n  return (\n    &lt;div&gt;\n      &lt;button\n        onClick={() =&gt; {\n          alert(`Your clipboard contains: ${clipboard}`);\n        }}\n      &gt;\n        Read my clipboard\n      &lt;\/button&gt;\n\n      &lt;button\n        onClick={() =&gt; {\n          setClipboard(`Random number: ${Math.random()}`);\n        }}\n      &gt;\n        Copy something new\n      &lt;\/button&gt;\n    &lt;\/div&gt;\n  );\n}\n<\/code><\/pre>\n\n\n\n<h2>Set document title ??\u200d?<\/h2>\n\n\n\n<p><a href=\"https:\/\/www.npmjs.com\/package\/@rehooks\/document-title\">@rehooks\/document-title<\/a>&nbsp;allows you to set the page title simple calling it from a component and passing the title string:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>import useDocumentTitle from '@rehooks\/document-title';\n\nfunction MyComponent() {\n  useDocumentTitle('Page Title');\n  return &lt;div\/&gt;;\n}\n<\/code><\/pre>\n\n\n\n<h2>Conclusion<\/h2>\n\n\n\n<p>These are some of the hooks that I use and save me a lot of time.<br>Do you know some incredible hooks? Comment here!<br>All the best!<\/p>\n","protected":false},"excerpt":{"rendered":"<p>The React community has created incredible hooks since this feature was released, I will show some of the 5 that I use and help me a lot.<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[30],"tags":[140],"_links":{"self":[{"href":"https:\/\/lvboard.infostore.in.ua\/index.php?rest_route=\/wp\/v2\/posts\/1815"}],"collection":[{"href":"https:\/\/lvboard.infostore.in.ua\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/lvboard.infostore.in.ua\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/lvboard.infostore.in.ua\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/lvboard.infostore.in.ua\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1815"}],"version-history":[{"count":1,"href":"https:\/\/lvboard.infostore.in.ua\/index.php?rest_route=\/wp\/v2\/posts\/1815\/revisions"}],"predecessor-version":[{"id":1816,"href":"https:\/\/lvboard.infostore.in.ua\/index.php?rest_route=\/wp\/v2\/posts\/1815\/revisions\/1816"}],"wp:attachment":[{"href":"https:\/\/lvboard.infostore.in.ua\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1815"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/lvboard.infostore.in.ua\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1815"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/lvboard.infostore.in.ua\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1815"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}