<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: 砖家</title>
    <description>The latest articles on DEV Community by 砖家 (@brickspert).</description>
    <link>https://dev.to/brickspert</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F774913%2Ffe45938a-aab3-47af-b057-f10c858221fb.jpeg</url>
      <title>DEV Community: 砖家</title>
      <link>https://dev.to/brickspert</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/brickspert"/>
    <language>en</language>
    <item>
      <title>ahooks 3.0 is coming! a high-quality and reliable React Hooks library!</title>
      <dc:creator>砖家</dc:creator>
      <pubDate>Wed, 15 Dec 2021 12:53:55 +0000</pubDate>
      <link>https://dev.to/brickspert/ahooks-30-is-coming-a-high-quality-and-reliable-react-hooks-library-2hif</link>
      <guid>https://dev.to/brickspert/ahooks-30-is-coming-a-high-quality-and-reliable-react-hooks-library-2hif</guid>
      <description>&lt;p&gt;&lt;a href="https://github.com/alibaba/hooks"&gt;ahooks&lt;/a&gt; is an open-source React Hooks library that encapsulates a large number of useful Hooks. In the current React project development, a useful React Hooks library is indispensable, hope ahooks can be your choice.&lt;br&gt;
​&lt;br&gt;
&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--Q1vqTBDs--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/6srhkkk9l8v1xtdp9h8v.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--Q1vqTBDs--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/6srhkkk9l8v1xtdp9h8v.png" alt="Image description" width="880" height="313"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Since the first version of ahooks (umi hooks) was released in August 2019, it has undergone 2 years of development. and has been recognized by many developers in the community. The current achievements of ahooks 2.0 mainly include:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Used by thousands of front-end applications within Alibaba Group&lt;/li&gt;
&lt;li&gt;Developed 60+ Hooks&lt;/li&gt;
&lt;li&gt;npm &amp;amp; tnpm weekly downloads 70k+&lt;/li&gt;
&lt;li&gt;GitHub star 7.4k&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;In the development process of the past two years, with a deeper understanding of React Hooks, we can see many deficiencies in the design of ahooks 2.0. In this context, we decided to develop version 3.0.&lt;/p&gt;

&lt;p&gt;The goal of ahooks 3.0 is to build a high-quality and reliable React Hooks library. We hope to become a stable and fundamental dependency like lodash. Compared with 2.0, it has the following advantages:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Fully support SSR&lt;/strong&gt;&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;New useRequest&lt;/strong&gt;&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;All output function references are fixed to avoid closure problems&lt;/strong&gt;&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;DOM Hooks support dynamic target&lt;/strong&gt;&lt;/li&gt;
&lt;li&gt;More appropriate API design&lt;/li&gt;
&lt;li&gt;Solved the problem in Strict Mode&lt;/li&gt;
&lt;li&gt;Solved the problem in react-refresh (HRM) mode&lt;/li&gt;
&lt;li&gt;Added more Hooks&lt;/li&gt;
&lt;li&gt;Fixed many known issues&lt;/li&gt;
&lt;/ul&gt;
&lt;h2&gt;
  
  
  Support SSR
&lt;/h2&gt;

&lt;p&gt;React Hooks generally encounter two problems, "DOM/BOM missing" and "useLayoutEffect warning" in SSR scenarios. ahooks v3.0 completely solves these two problems, and you can safely use ahooks in SSR scenarios with no worries.&lt;/p&gt;

&lt;p&gt;For more information, please refer to "&lt;a href="https://ahooks.js.org/guide/blog/ssr/"&gt;React Hooks &amp;amp; SSR&lt;/a&gt;"&lt;/p&gt;
&lt;h2&gt;
  
  
  New useRequest
&lt;/h2&gt;

&lt;p&gt;useRequest is the Hook with the highest usage of ahooks and also the Hook with the most issues. The biggest problem of the previous useRequest is:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Code splitting is inappropriate, all features are mixed in one file. It's very complicated to make changes to the code.&lt;/li&gt;
&lt;li&gt;Some features were not fully researched before they were released, resulting in inappropriately designed features that could not be dropped after being released.&lt;/li&gt;
&lt;li&gt;The logic of pagination and loadMore were combined together, resulting in super complex TS types.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;ahooks v3.0 completely rewrote useRequest:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Organized the source code through a plug-in pattern, the core code is extremely simple, all advanced features are implemented with plug-ins.&lt;/li&gt;
&lt;li&gt;Carefully researched all the features provided to ensure that the released features are the optimal solutions. Will progressively add new features which are still under research.&lt;/li&gt;
&lt;li&gt;All options support dynamic changes&lt;/li&gt;
&lt;li&gt;Removed the pagination and loadMore logic, separated out some other Hooks to provide corresponding features.&lt;/li&gt;
&lt;li&gt;Avoid TS type overload, it's more convenient for encapsulating more advanced Hooks based on useRequest.&lt;/li&gt;
&lt;li&gt;Solve a large number of remaining issues.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;For more information, please refer to "&lt;a href="https://ahooks.js.org/guide/upgrade/#new-userequest"&gt;New useRequest&lt;/a&gt;"&lt;/p&gt;
&lt;h2&gt;
  
  
  Special handling of functions to avoid closure problems
&lt;/h2&gt;

&lt;p&gt;ahooks v3 tries its best to help everyone avoid the closure problem by specially processing the input and output functions. I think this ability is a more radical aspect of ahooks, but it does provide users with a very good experience.&lt;/p&gt;

&lt;p&gt;1.&lt;strong&gt;All the output functions of ahooks, the references are stable&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;state&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;setState&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;useState&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;As we all know, the reference of the &lt;code&gt;setState&lt;/code&gt; function returned by &lt;code&gt;React.useState&lt;/code&gt; is fixed, and there is no need to consider weird problems when using it, and there is no need to put &lt;code&gt;setState&lt;/code&gt; in the dependencies of other Hooks.&lt;br&gt;
All functions returned by ahooks v3.0 Hooks have the same characteristics as &lt;code&gt;setState&lt;/code&gt;, the reference will not change, just feel free to use it.&lt;br&gt;
​&lt;/p&gt;

&lt;p&gt;2.&lt;strong&gt;For all user input functions, always use the latest one&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;For the received function, ahooks v3 will do a special process to ensure that the function called each time is always the latest.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;state&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;setState&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;useState&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;

&lt;span class="nx"&gt;useInterval&lt;/span&gt;&lt;span class="p"&gt;(()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;state&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;},&lt;/span&gt; &lt;span class="mi"&gt;1000&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;For example, in the above example, the function called by &lt;code&gt;useInterval&lt;/code&gt; at any time is always the latest, that is, the &lt;code&gt;state&lt;/code&gt; is always the latest.&lt;/p&gt;

&lt;p&gt;For more information, please refer to "&lt;a href="https://ahooks.js.org/guide/blog/function"&gt;ahooks function specification&lt;/a&gt;"&lt;/p&gt;

&lt;h2&gt;
  
  
  More
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;DOM Hooks support the dynamic target. For related documents, please refer to "&lt;a href="https://ahooks.js.org/guide/dom"&gt;Hooks of DOM specification&lt;/a&gt;"&lt;/li&gt;
&lt;li&gt;v3 fixes some problems in strict mode. Refer to "&lt;a href="https://ahooks.js.org/guide/blog/strict"&gt;React Hooks &amp;amp; strict mode&lt;/a&gt;"&lt;/li&gt;
&lt;li&gt;v3 fixes some problems in react-refresh (HRM) mode. Refer to "&lt;a href="https://ahooks.js.org/guide/blog/hmr"&gt;React Hooks &amp;amp; react-refresh (HMR)&lt;/a&gt;"&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;For more changes, please refer to "&lt;a href="https://ahooks.js.org/guide/upgrade"&gt;v2 to v3&lt;/a&gt;"&lt;br&gt;
​&lt;/p&gt;
&lt;h2&gt;
  
  
  Lastly
&lt;/h2&gt;

&lt;p&gt;The slogan of ahooks v3.0 is "a high-quality and reliable React Hooks library". This is also the goal for ahooks. Hope ahooks will become one of the essential fundamental libraries in everyone's toolkit.&lt;br&gt;
​&lt;br&gt;
Thanks to the contributors and users of ahooks! Welcome to try v3.0!&lt;br&gt;
​&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nv"&gt;$ &lt;/span&gt;npm &lt;span class="nb"&gt;install&lt;/span&gt; &lt;span class="nt"&gt;--save&lt;/span&gt; ahooks@next
&lt;span class="c"&gt;# or&lt;/span&gt;
&lt;span class="nv"&gt;$ &lt;/span&gt;yarn add ahooks@next
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;Document: &lt;a href="https://ahooks.js.org"&gt;https://ahooks.js.org&lt;/a&gt;​&lt;/li&gt;
&lt;li&gt;Source code: &lt;a href="https://github.com/alibaba/hooks"&gt;https://github.com/alibaba/hooks&lt;/a&gt;
&lt;/li&gt;
&lt;/ul&gt;

</description>
      <category>react</category>
      <category>frontend</category>
    </item>
  </channel>
</rss>
