@shined/react-use
aims to guide a new programming paradigm in reshaping React development. It helps developers improve development efficiency and foster better programming habits by providing numerous high-quality, semantic Hooks. This reduces the direct reliance on useEffect
and useState
, while also encouraging developers to gradually adapt to a "Hooks first" React development (programming) paradigm.
Below are some common usage guidelines to help you better utilize @shined/react-use
.
The following includes only a part of the common scenarios, following best practices. Please choose according to the specific situation.
useState
is a Hook used in React for managing component state, and it's something almost every React developer will use.
However, in lower versions of React, useState
may lead to unexpected behavior. For instance, in React <= 17, calling setState
after a component has unmounted will throw confusing warnings (please refer to Safe State). Moreover, React uses shallow comparison internally to determine if the state has changed, which could lead to unnecessary component re-renders, thereby affecting performance.
useSafeState
is designed as a direct replacement for useState
, aimed at avoiding the warning issues in lower versions of React and following the official practices to keep consistent behavior with useState
in higher versions of React.
At the same time, it also has an optional performance optimization feature (deep
option, deep compares states to confirm changes before updating, default false
).
For more details, please refer to Safe State and useSafeState.
useBoolean
is used for managing boolean state, providing a series of semantic operation functions, such as toggle
, setTrue
, setFalse
, etc., using useSafeState
underneath to ensure state safety.
For more details, see useBoolean.
useCounter
is used to manage number type states, offering a series of semantic operation functions, such as inc
, dec
, set
, etc., using useSafeState
underneath to ensure state safety.
For more details, see useCounter.
useEffect
is one of the most basic and commonly used Hooks in React. However, in general, we do not recommend using it directly. This is because its usage is relatively primitive and itโs prone to issues such as difficulty in controlling side effects or side effects not meeting expectations.
@shined/react-use
provides a series of high-quality, semantic Hooks to replace some use cases of useEffect
.
We might use useEffect
like this, which is functionally equivalent to executing doSomething()
once when the component is mounted.
Or, when we need to perform some asynchronous operations at mount time and need to obtain the result, we would usually wrap it in an async function to execute the asynchronous operation.
The above code is logically sound, but it has many issues and hidden dangers, such as poor code readability, lack of semantics, difficult to maintain in the future, and possible accidental return of cleanup functions, and it also does not support asynchronous functions well. It is recommended to replace it with the more semantic useMount
, which supports asynchronous functions.
For more details, see useMount.
useUnmount
is used to perform some operations when the component is unmounted, such as cleaning up side effects. It is similar to useMount
but executes at a different time.
For more details, see useUnmount.
useUpdateEffect
is used to perform some operations when the component updates, such as listening for changes in certain states and performing operations, but ignores the initial render. It is suitable for scenarios where immediate execution of side effects is not necessary.
For more details, see useUpdateEffect.
useEffectOnce
is used to perform an operation once when the component mounts and once when it unmounts. It is suitable for scenarios where only a single side effect execution is required. Essentially, useEffectOnce
is a combination of useMount
and useUnmount
.
For more details, see useEffectOnce.
useAsyncEffect
is used to perform asynchronous operations when the state changes. It is suitable for scenarios where you need to listen to state changes and perform asynchronous operations.
For more details, see useAsyncEffect.
It's recommended to use useAsyncFn
for handling asynchronous operations. It automatically manages states like loading and has a complete lifecycle.
For more details, see useAsyncFn.
We recommend using useQuery
for handling data fetching. It provides a series of semantic operations such as run
, cancel
, refresh
, etc., while also supporting the automatic management of loading
, error
, and data
states.
For more details, see useQuery.
We recommend using the useDebouncedFn
and useThrottledFn
hooks for handling common debounce and throttle functionalities. There are also useDebouncedEffect and useThrottledEffect hooks available for managing debounce and throttle side effects. However, we generally prefer the former.
For more details, see useDebouncedFn and useThrottledFn.
It is recommended to use usePagination
for handling pagination logic. This hook provides a series of semantic operations functions such as next
, prev
, go
, etc., and it supports customization of the page number, number of items per page, and total number of items.
For more details, see usePagination.
useClipboard
is used to copy text to the clipboard. It is suitable for scenarios that require copying text to the clipboard. By default, it uses the Clipboard API. If the browser does not support it, it will gracefully downgrade to document.execCommand('copy')
.
For more details, see useClipboard.
useDateFormat
is for formatting time, offering an extra lightweight, flexible, unified user experience solution choice, suitable for scenarios that require time formatting and supports unicode standard tokens.
For more details, see useDateFormat.
In day-to-day development, we often use setTimeout
and setInterval
to handle timing tasks. Direct use can be relatively cumbersome and requires developers to manually clear timers, which can easily lead to issues like forgetting to clear or not clearing in time.
@shined/react-use
provides the useTimeoutFn
and useIntervalFn
hooks to handle timing tasks, automatically clearing timers to avoid issues related to forgetting or not clearing timers in time.
For more details, see useTimeoutFn and useIntervalFn.
useEventListener
is used to add an event listener when a component mounts and automatically remove it when the component unmounts. It is suitable for scenarios that require adding event listeners. Any object that implements the EventTarget
interface can be passed as the first parameter, such as window
, document
, ref.current
, etc.
For more details, see useEventListener.
We often need to call browser APIs to implement certain functionalities, including but not limited to:
Direct operation of APIs can make the code complex and hard to maintain. Due to the compatibility issues of APIs, developers not only have more cognitive load in identifying compatibility situations but may also need to increase the complexity of code to achieve compatibility (e.g., using legacy APIs for as much compatibility as possible). Additionally, numerous details must be considered to adapt to React component-based development.
For example, the Fullscreen API is implemented differently in various browsers and versions, the Battery Status API is only supported in some browsers, and the EyeDropper API is currently only available in the latest versions of Chrome and Edge.
Fortunately, @shined/react-use
has packaged many common browser APIs to provide a better user experience. Many browser API-related Hooks internally use useSupported to uniformly return the API's support status, making it more convenient for developers to use browser APIs.
To learn more about available browser API Hooks, please visit the Browser category on the Hooks List page.
@shined/react-use
aims to provide better server-side rendering support. All Hooks are server-side rendering compatible and do not produce side effects.
useIsomorphicLayoutEffect
is used to switch between useLayoutEffect
during server-side rendering and useEffect
during client-side rendering. It is suitable for scenarios that require executing synchronous side effects during server-side rendering.
For more details, see useIsomorphicLayoutEffect.
useCallback
and useMemo
are Hooks in React used for performance optimization, commonly used to cache functions and values to avoid unnecessary recalculations.
However, in practice, besides performance optimization, we may also need to ensure the stability of references to avoid unnecessary side effects and other issues. According to the official documentation, useCallback
and useMemo
are intended for performance optimization and do not guarantee stability of references, which may lead to unstable behavior in some scenarios.
If you need to optimize performance while ensuring the stability of functions and results, you might want to try useStableFn
and useCreation
.
useStableFn
is used to ensure the stability of function references. It is suitable for scenarios that require stable function references, such as callbacks passed to child components.
For more details, see useStableFn.
useCreation
is used for initialization operations. It is suitable for scenarios that require initialization operations to be performed only once, such as initializing complex objects, lengthy operations, etc. Besides performance optimization, useCreation
also ensures that the result remains stable across different rendering cycles.
For more details, see useCreation.
If you need to encapsulate a custom Hook, or require more advanced features, please refer to the following advanced guide.
useStableFn
is used to ensure the stability of function references, suitable for scenarios where it is necessary to keep function references stable, such as callbacks passed to child components. Functions exposed by Hooks should ideally be wrapped with useStableFn
.
For more details, refer to useStableFn.
useLatest
is used to access the most recent value, suitable for scenarios where obtaining the latest state is needed, such as in asynchronous operations. It is commonly used in conjunction with useStableFn
to achieve caching of stable callback functions while obtaining the latest state.
For more details, refer to useLatest.
useTargetElement
is used to obtain the target element, suitable for scenarios where accessing a target element is needed, such as in custom Hooks to ensure a consistent user experience.
For more details, refer to useTargetElement and ElementTarget.
useCreation
is used for initialization operations, suitable for scenarios where it's necessary to ensure that initialization operations are performed only once, such as initializing complex objects or time-consuming operations. In addition to performance optimization, useCreation
also ensures that results remain stable across different render cycles.
For more details, refer to useCreation.
useSupported
is used to check the support status of browser APIs, suitable for scenarios where it's necessary to determine browser API support.
For more details, refer to useSupported.
usePausable
is used to create a Pausable instance, giving Hooks the capability to be paused and resumed, suitable for scenarios where pausing and resuming is needed.
For more details, refer to usePausable.
useGetterRef
exposes a function to access the latest value of ref.current
, suitable for scenarios where it's necessary to store state without triggering re-renders, while still needing to access the most recent value.
For more details, refer to useGetterRef.