<br />
<b>Notice</b>:  Function _load_textdomain_just_in_time was called <strong>incorrectly</strong>. Translation loading for the <code>all-in-one-seo-pack</code> domain was triggered too early. This is usually an indicator for some code in the plugin or theme running too early. Translations should be loaded at the <code>init</code> action or later. Please see <a href="https://developer.wordpress.org/advanced-administration/debug/debug-wordpress/">Debugging in WordPress</a> for more information. (This message was added in version 6.7.0.) in <b>/home/fes/web/olgamatosova.sfdevserver.com/public_html/blog/wp-includes/functions.php</b> on line <b>6131</b><br />
<br />
<b>Notice</b>:  Function _load_textdomain_just_in_time was called <strong>incorrectly</strong>. Translation loading for the <code>wp-diary</code> domain was triggered too early. This is usually an indicator for some code in the plugin or theme running too early. Translations should be loaded at the <code>init</code> action or later. Please see <a href="https://developer.wordpress.org/advanced-administration/debug/debug-wordpress/">Debugging in WordPress</a> for more information. (This message was added in version 6.7.0.) in <b>/home/fes/web/olgamatosova.sfdevserver.com/public_html/blog/wp-includes/functions.php</b> on line <b>6131</b><br />
{"id":20,"date":"2024-05-08T10:00:17","date_gmt":"2024-05-08T10:00:17","guid":{"rendered":"https:\/\/olgamatosova.sfdevserver.com\/blog\/?p=20"},"modified":"2024-05-08T10:55:13","modified_gmt":"2024-05-08T10:55:13","slug":"react-19-what-do-you-need-to-know-about-new-hooks","status":"publish","type":"post","link":"https:\/\/olgamatosova.sfdevserver.com\/blog\/?p=20","title":{"rendered":"React 19: What do you need to know about new hook Use"},"content":{"rendered":"\n<p>Understanding the future of  React <\/p>\n\n\n\n<p>React19 has brought new&nbsp;<a href=\"https:\/\/www.geeksforgeeks.org\/reactjs-hooks\/?ref=header_search\" target=\"_blank\" rel=\"noreferrer noopener\">Hooks<\/a>&nbsp;to the board to streamline the development process. Here is the breakdown of newly introduced Hooks.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">The new&nbsp;<code><strong><mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-vivid-red-color\">use()<\/mark><\/strong><\/code>&nbsp;hook<\/h2>\n\n\n\n<p>React19 will introduce a new hook called&nbsp;<code>use()<\/code>. This hook will simplify how we use promises, async code, and context.<\/p>\n\n\n\n<p>Here is the syntax of hook:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"jsx\" class=\"language-jsx\"><strong>import<\/strong> { use } <strong>from<\/strong> 'react';\n\n<strong>function<\/strong> MessageComponent({ messagePromise }) {\n    <strong>const<\/strong> message = use(messagePromise);\n    \/\/ ...\n}<\/code><\/pre>\n\n\n\n<p>The good news is that this hook can be used for data fetching. Here&#8217;s an example of fetching data when the component mounts and when a button is clicked. Note that the useEffect hook is not used in the code:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"jsx\" class=\"language-jsx\">import * as React from 'react';\nimport { useState, use, Suspense } from 'react';\nimport { faker } from '@faker-js\/faker';\n\nexport const App = () => {\n  const [newsPromise, setNewsPromise] = useState(() => fetchNews());\n\n  const handleUpdate = () => {\n    fetchNews().then((news) => {\n      setNewsPromise(Promise.resolve(news));\n    });\n  };\n\n  return (\n    &lt;>\n      &lt;h3>\n        Here is the news &lt;button onClick={handleUpdate}>Refresh&lt;\/button>\n      &lt;\/h3>\n      &lt;NewsContainer newsPromise={newsPromise} \/>\n    &lt;\/>\n  );\n};\n\nlet news = [...new Array(4)].map(() => faker.lorem.sentence());\n\nconst fetchNews = () =>\n  new Promise&lt;string[]>((resolve) =>\n    \/\/ simulate data fetching with a setTimeout\n    setTimeout(() => {\n      \/\/ add one more headline at each refresh\n      news.unshift(faker.lorem.sentence());\n      resolve(news);\n    }, 1000)\n  );\n\nconst NewsContainer = ({ newsPromise }) => (\n  &lt;Suspense fallback={&lt;p>Fetching the news...&lt;\/p>}>\n    &lt;News newsPromise={newsPromise} \/>\n  &lt;\/Suspense>\n);\n\nconst News = ({ newsPromise }) => {\n  const news = use&lt;string[]>(newsPromise);\n  return (\n    &lt;ul>\n      {news.map((title, index) => (\n        &lt;li key={index}>{title}&lt;\/li>\n      ))}\n    &lt;\/ul>\n  );\n};\n<\/code><\/pre>\n\n\n\n<p>Let&#8217;s understand the code:<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li><code>fetchNews<\/code>\u00a0is responsible for the GET request.<\/li>\n\n\n\n<li>We are using the\u00a0<code>use<\/code>\u00a0hook to execute the\u00a0<code>fetchNews<\/code>\u00a0instead of using the\u00a0<code>useEffect<\/code>\u00a0or\u00a0<code>useState<\/code>\u00a0hooks.<\/li>\n\n\n\n<li>The return of the\u00a0<code>useState<\/code>\u00a0hook is\u00a0<code>news<\/code>\u00a0which will have the response of the GET request.<\/li>\n\n\n\n<li>In the return block, we are using\u00a0<code>news<\/code>\u00a0to map over it and create the list<\/li>\n<\/ol>\n\n\n\n<p>Remember this warning in the &lt;Suspense> documentation?<\/p>\n\n\n\n<p class=\"has-white-color has-cyan-bluish-gray-background-color has-text-color has-background has-link-color wp-elements-16ed0db155b8df05fb6695b70354b94d\">Data fetching with Suspense support without a self-sufficient framework is not supported. <\/p>\n\n\n\n<p>But in React 19, this is no longer relevant.<\/p>\n\n\n\n<p>The use hook has an interesting feature: unlike other React hooks, it can be called inside loops and conditional operators, such as if.<\/p>\n\n\n\n<p>Does this mean that for data fetching on the client side, it is no longer necessary to use third-party libraries, such as TanStack Query? Well, we still have to figure this out, as TanStack Query does much more than just resolving promises.<\/p>\n\n\n\n<p>But this is a step in the right direction, significantly simplifying the creation of single-page applications working with REST or GraphQL APIs. I&#8217;m excited about this new hook!<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Understanding the future of React React19 has brought new&nbsp;Hooks&nbsp;to the board to streamline the development process. Here is the breakdown of newly introduced Hooks. The new&nbsp;use()&nbsp;hook React19 will introduce a new hook called&nbsp;use(). This hook will simplify how we use promises, async code, and context. Here is the syntax of hook: The good news is [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"om_disable_all_campaigns":false,"footnotes":""},"categories":[3],"tags":[6,5],"class_list":["post-20","post","type-post","status-publish","format-standard","hentry","category-react","tag-hook","tag-react"],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/olgamatosova.sfdevserver.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/20","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/olgamatosova.sfdevserver.com\/blog\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/olgamatosova.sfdevserver.com\/blog\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/olgamatosova.sfdevserver.com\/blog\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/olgamatosova.sfdevserver.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=20"}],"version-history":[{"count":5,"href":"https:\/\/olgamatosova.sfdevserver.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/20\/revisions"}],"predecessor-version":[{"id":29,"href":"https:\/\/olgamatosova.sfdevserver.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/20\/revisions\/29"}],"wp:attachment":[{"href":"https:\/\/olgamatosova.sfdevserver.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=20"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/olgamatosova.sfdevserver.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=20"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/olgamatosova.sfdevserver.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=20"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}