自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

譯文 | 值得推薦的十大 React Hook 庫(kù)

新聞 前端
React Hook來(lái)了,并在暴風(fēng)雨中占領(lǐng)了React社區(qū)。自最初發(fā)布以來(lái)已經(jīng)有一段時(shí)間了,這意味著有很多支持庫(kù)。

 React Hook來(lái)了,并在暴風(fēng)雨中占領(lǐng)了React社區(qū)。自最初發(fā)布以來(lái)已經(jīng)有一段時(shí)間了,這意味著有很多支持庫(kù)。在搜索與React相關(guān)的內(nèi)容時(shí),很難不看到“ hook”這個(gè)詞語(yǔ)。如果你還沒有遇到的話,應(yīng)該盡快將它們加入代碼庫(kù)學(xué)習(xí)起來(lái)。它們將使您的編碼生活變得更加輕松和愉快。

在React開發(fā)中,保持干凈的代碼風(fēng)格,可讀性,可維護(hù)性,更少的代碼行以及可重用性至關(guān)重要。該博客將向您展示應(yīng)當(dāng)被立即開始使用的 十大React Hook庫(kù) 。不用再拖延了,讓我們開始吧。

1.use-http

use-http 是一個(gè)非常有用的庫(kù),可用來(lái)替代Fetch API。它使您的編碼更簡(jiǎn)單易懂,更精確地講是數(shù)據(jù)操作部分。 use-http 本身使用TypeScript,甚至支持SSR和GraphQL。它返回響應(yīng),加載,錯(cuò)誤數(shù)據(jù)和不同的請(qǐng)求方法,例如Get,Post,Put,Patch和Delete。

它提供的主要功能是:

  • 請(qǐng)求/響應(yīng)攔截器
  • 暫停(目前處于實(shí)驗(yàn)狀態(tài))
  • 重發(fā)功能
  • 緩存

CodeSandbox示例和Youtube視頻以及GitHub自述文件都對(duì)此進(jìn)行了很好的記錄。

用法示例:

  1. import useFetch from "use-http" 
  2.  
  3. const Example = () => { 
  4.   const [todos, setTodos] = useState([]) 
  5.   const { get, post, response, loading, error } = useFetch("https://example.com"
  6.  
  7.   useEffect(() => { get("/todos") }, []) 
  8.  
  9.   const addTodo = async () => { 
  10.       await post("/todos", { title: "example todo" }); 
  11.       if (response.ok) setTodos([...todos, newTodo]) 
  12.   } 
  13.  
  14.   return ( 
  15.     <> 
  16.       <button onClick={addTodo}>Add Todo</button> 
  17.       {error && 'Error!'
  18.       {loading && 'Loading...'
  19.       {todos.map(todo => ( 
  20.         <span key={todo.id}>{todo.title}</span> 
  21.       )} 
  22.     </> 
  23.   ); 
  24. }; 

2. useMedia

您是否需要一種跟蹤C(jī)SS媒體查詢的方法?useMedia提供一個(gè)簡(jiǎn)單的方法解決這個(gè)問題。這是一個(gè)能夠準(zhǔn)確跟蹤的感官鉤子。在任何應(yīng)用程序或網(wǎng)站上,媒體查詢以及響應(yīng)能力都非常重要。

它以TypeScript編寫。該軟件包具有定義明確的文檔,它的用法以及測(cè)試方法也解釋的非常好。

用法示例:

  1. import useMedia from 'use-media'
  2.  
  3. const Example = () => { 
  4.   const isWide = useMedia({minWidth: '1000px'}); 
  5.  
  6.   return ( 
  7.     <span> 
  8.       Screen is wide: {isWide ? "WideScreen" : "NarrowScreen"
  9.     </span> 
  10.   ); 
  11. }; 

3.Constate

Constate可將本地狀態(tài)提升到React Context。這意味著可以以最小的努力輕松地將任何組件的任何狀態(tài)提升到上下文。如果您想在多個(gè)位置使用相同的狀態(tài),或者為多個(gè)組件提供相同的狀態(tài),這很有用。該名稱來(lái)自合并 Con text和 state 。

它是基于typescript實(shí)現(xiàn)的,而且?guī)旆浅5男 km然該文檔不是很詳細(xì),但是看例子,我們就可以輕易地學(xué)會(huì)使用。

用法示例:

  1. import React, { useState } from "react"
  2. import constate from "constate"
  3.  
  4. // custom hook 
  5. function useCounter() { 
  6.   const [count, setCount] = useState(0); 
  7.   const increment = () => setCount(prevCount => prevCount + 1); 
  8.   return { count, increment }; 
  9.  
  10. // hook passed in constate 
  11. const [CounterProvider, useCounterContext] = constate(useCounter); 
  12.  
  13. function Button() { 
  14.   // use the context 
  15.   const { increment } = useCounterContext(); 
  16.   return <button onClick={increment}>+</button>; 
  17.  
  18. function Count() { 
  19.   // use the context 
  20.   const { count } = useCounterContext(); 
  21.   return <span>{count}</span>; 
  22.  
  23. function App() { 
  24.   // wrap the component with provider 
  25.   return ( 
  26.     <CounterProvider> 
  27.       <Count /> 
  28.       <Button /> 
  29.     </CounterProvider> 
  30.   ); 

4. Redux hooks

Redux是許多(即使不是全部)React開發(fā)人員的知名工具。在整個(gè)應(yīng)用程序中,它用作全局狀態(tài)管理器。在React的最初版本發(fā)布幾個(gè)月后,它就隨hooks而出現(xiàn)了。它通過利用現(xiàn)有的connect()方法 提供了 一種 替代HOC(高階組件)模式的方法。

提供的最著名的掛鉤是:

  • useSelector
  • useDispatch
  • useStore

該文檔非常好,雖然有點(diǎn)復(fù)雜,但是它將為您提供開始使用它們所需的任何信息。

用法示例:

  1. import  {useSelector, useDispatch} from "react-redux"
  2. import * as actions from "./actions"
  3.  
  4. const Example = () => { 
  5. const dispatch = useDispatch() 
  6. const counter = useSelector(state => state.counter) 
  7.  
  8. return ( 
  9. <div> 
  10.    <span> 
  11.      {counter.value} 
  12.    </span> 
  13.    <button onClick={() => dispatch(actions.incrementCounter)}> 
  14.      Counter +1 
  15.    </button> 
  16. </div> 
  17. ); 
  18. }; 

5. React hook form

React hook form 是一個(gè)表單鉤子庫(kù),類似于Formik和Redux表單,但是更好!憑借其更簡(jiǎn)單的語(yǔ)法,速度,更少的重新渲染數(shù)和更好的可維護(hù)性,它開始爬上GitHub的階梯。

它的體積很小,并且是考慮到性能而構(gòu)建起來(lái)的。該庫(kù)甚至提供了非常棒的表單生成器!它是React鉤子庫(kù)中GitHub star數(shù)量最多的庫(kù)之一(14.8k)。

用法示例:

  1. import React from "react"
  2. import { useForm } from "react-hook-form"
  3.  
  4. function App() { 
  5.   const { register, handleSubmit, errors } = useForm(); 
  6.   const onSubmit = (data) => { 
  7.     // logs {firstName:"exampleFirstName", lastName:"exampleLastName"} 
  8.     console.log(data); 
  9.   }; 
  10.  
  11.   return ( 
  12.     <form onSubmit={handleSubmit(onSubmit)}> 
  13.       <input name="firstName" ref={register} /> 
  14.       <input name="lastName" ref={register({ required: true })} /> 
  15.       {errors.lastName && <span>"Last name is a required field."</span>} 
  16.       <input name="age" ref={register({ required: true })} /> 
  17.       {errors.age && <span>"Please enter number for age."</span>} 
  18.       <input type="submit" /> 
  19.     </form> 
  20.   ); 

6. useDebounce

useDebounce表示一個(gè)用于防抖的小鉤子。它用于將功能的執(zhí)行推遲到以后。常用于獲取數(shù)據(jù)的輸入框和表格中。

用法示例:

  1. import React, { useState } from "react"
  2. import { useDebounce } from "use-debounce"
  3.  
  4. export default function Input() { 
  5.   const [text, setText] = useState("Hello"); 
  6.   const [value] = useDebounce(text, 1000); 
  7.  
  8.   return ( 
  9.     <div> 
  10.       <input 
  11.         defaultValue={"Hello"
  12.         onChange={(e) => { 
  13.           setText(e.target.value); 
  14.         }} 
  15.       /> 
  16.       <p>Value: {text}</p> 
  17.       <p>Debounced value: {value}</p> 
  18.     </div> 
  19.   ); 

7. useLocalStorage

useLocalStorage是一個(gè)小鉤子,與上面的鉤子一樣。它對(duì)于在localStorage中提取和設(shè)置數(shù)據(jù)非常有用。使用它之后操作變得很容易。

提供自動(dòng)JSON序列化和同步的功能

文檔以高質(zhì)量的方式編寫,并且通過擴(kuò)展示例可以完全理解。

用法示例:

  1. import React, { useState } from "react"
  2. import { writeStorage } from '@rehooks/local-storage'
  3.  
  4. export default function Example() { 
  5.   let counter = 0
  6.   const [counterValue] = useLocalStorage('counterValue'); 
  7.  
  8.   return ( 
  9.     <div> 
  10.       <span>{counterValue}</span> 
  11.       <button onClick={() => writeStorage('i', ++counter)}> 
  12.         Click Me 
  13.       </button> 
  14.     </div> 
  15.   ); 

8. usePortal

usePortal使得創(chuàng)建下拉菜單,彈出層,通知彈出窗口,提示等變得非常容易!它提供了在應(yīng)用程序的DOM層次結(jié)構(gòu)之外創(chuàng)建元素的功能。

它還提供了門戶樣式和大量其他選項(xiàng)的完全定制。

編寫的文檔非常好,其中展示了許多示例,這些示例足夠用于開始使用庫(kù)/自己做鉤子。

用法示例:

  1. import React, { useState } from "react"
  2. import usePortal from "react-useportal"
  3.  
  4. const Example = () => { 
  5.     const { ref, openPortal, closePortal, isOpen, Portal } = usePortal() 
  6.  
  7.     return ( 
  8.       <> 
  9.          <button ref={ref} onClick={() => openPortal()}> 
  10.             Open Portal 
  11.          </button> 
  12.           {isOpen && ( 
  13.             <Portal> 
  14.               <p> 
  15.                 This Portal handles its own state.{' '
  16.                 <button onClick={closePortal}>Close me!</button>, hit ESC or 
  17.                 click outside of me. 
  18.               </p> 
  19.             </Portal> 
  20.           )} 
  21.        </> 
  22.  ) 

9. useHover

它確定是否正在處于hover的React元素。簡(jiǎn)單易用。該庫(kù)很小,易于使用,但如果您有足夠的創(chuàng)造力,它可能會(huì)很強(qiáng)大。

它還提供了懸停效果的延遲功能。支持TypeScript。雖然文檔沒有那么詳細(xì),但是它將向您展示如何正確地使用它。

用法示例:

  1. import useHover from "react-use-hover"
  2.  
  3. const Example = () => { 
  4.   const [isHovering, hoverProps] = useHover(); 
  5.   return ( 
  6.     <> 
  7.       <span {...hoverProps} aria-describedby="overlay">Hover me</span> 
  8.       {isHovering ? <div> I’m a little tooltip! </div> : null
  9.     </> 
  10.   ); 

10. React router hooks

React router 是React最受歡迎的庫(kù)之一。它用于路由和獲取應(yīng)用程序URL歷史記錄等。它與Redux一起實(shí)現(xiàn)了用于獲取此類有用的數(shù)據(jù)。

提供的掛鉤有:

  • useHistory
  • useLocation
  • useParams
  • useRouteMatch

它的名字很不言自明。UseHistory將獲取應(yīng)用程序歷史記錄和方法的數(shù)據(jù),例如push一個(gè)new route。UseLocation將返回代表當(dāng)前URL的對(duì)象。UseParams將返回當(dāng)前URL的參數(shù)的鍵-值對(duì)的對(duì)象。最后,useRouteMatch將嘗試將當(dāng)前URL與給定URL進(jìn)行匹配,給定URL可以是字符串,也可以是具有不同選項(xiàng)的對(duì)象。

文檔很好,寫了很多例子。

用法示例:

  1. import { useHistory, useLocation, useRouteMatch } from "react-router-dom"
  2.  
  3. const Example = () => { 
  4. let history = useHistory(); 
  5. let location = useLoction(); 
  6. let isMatchingURL = useRouteMatch("/post/11"); 
  7.  
  8. function handleClick() { 
  9. history.push("/home"); 
  10.  
  11. return ( 
  12.     <div> 
  13.         <span>Current URL: {location.pathname}</span> 
  14.         {isMatchingURL ? <span>Matching provided URL! Yay! </span> : null
  15.         <button type="button" onClick={handleClick}> 
  16.             Go home 
  17.         </button> 
  18. </div> 
  19. ); 

還有更多的鉤子庫(kù),但是這些是我決定要談?wù)摰?。?qǐng)嘗試一下,我保證您不會(huì)后悔。如果您確實(shí)很喜歡它們,請(qǐng)以任何方式去支持他們。hooks仍然是執(zhí)行此操作的一種相對(duì)較新的方法。在接下來(lái)的幾個(gè)月中,我們希望有更多出色的庫(kù)和鉤子示例浮出水面。

希望您發(fā)現(xiàn)這篇文章有趣,并且您學(xué)到了一些新東西。在進(jìn)一步探索hooks中玩得開心!發(fā)展愉快。

 

責(zé)任編輯:張燕妮 來(lái)源: 博客園
相關(guān)推薦

2025-04-09 08:08:56

2017-12-26 08:52:29

React庫(kù)DevOpsCSS

2022-03-03 09:30:00

安全容器工具

2024-11-18 00:18:18

2022-03-16 15:55:17

工業(yè)軟件物聯(lián)網(wǎng)

2021-05-14 09:49:47

React HookReact應(yīng)用

2021-04-12 11:09:13

React庫(kù)開發(fā)

2024-08-05 14:13:56

2020-10-21 11:34:49

React Hook庫(kù)

2017-01-05 15:06:23

2022-08-05 09:25:27

React庫(kù)JavaScript工具

2010-10-28 11:22:45

開源項(xiàng)目

2009-12-07 09:26:00

LinuxWindows

2023-06-27 07:31:02

動(dòng)畫庫(kù)React參數(shù)

2009-12-12 11:01:30

LinuxWindows系統(tǒng)特性

2022-04-24 08:00:00

開發(fā)UI組件

2021-01-06 15:58:45

智能手機(jī)折疊屏操作系統(tǒng)

2023-07-27 17:09:55

智能建筑物聯(lián)網(wǎng)5G

2018-12-11 04:05:26

邊緣計(jì)算數(shù)據(jù)物聯(lián)網(wǎng)

2024-12-02 13:29:46

點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號(hào)