mirror of
https://github.com/facebook/docusaurus.git
synced 2025-06-07 05:12:31 +02:00
refactor(theme-common): rename useDynamicCallback to useEvent (#7671)
This commit is contained in:
parent
9473508c33
commit
2c7012f706
7 changed files with 15 additions and 15 deletions
|
@ -5,7 +5,7 @@
|
||||||
* LICENSE file in the root directory of this source tree.
|
* LICENSE file in the root directory of this source tree.
|
||||||
*/
|
*/
|
||||||
import {useEffect} from 'react';
|
import {useEffect} from 'react';
|
||||||
import {useDynamicCallback, useShallowMemoObject} from '../utils/reactUtils';
|
import {useEvent, useShallowMemoObject} from '../utils/reactUtils';
|
||||||
|
|
||||||
type Options = MutationObserverInit;
|
type Options = MutationObserverInit;
|
||||||
|
|
||||||
|
@ -21,7 +21,7 @@ export function useMutationObserver(
|
||||||
callback: MutationCallback,
|
callback: MutationCallback,
|
||||||
options: Options = DefaultOptions,
|
options: Options = DefaultOptions,
|
||||||
): void {
|
): void {
|
||||||
const stableCallback = useDynamicCallback(callback);
|
const stableCallback = useEvent(callback);
|
||||||
|
|
||||||
// MutationObserver options are not nested much
|
// MutationObserver options are not nested much
|
||||||
// so this should be to memo options in 99%
|
// so this should be to memo options in 99%
|
||||||
|
|
|
@ -38,7 +38,7 @@ export {ThemeClassNames} from './utils/ThemeClassNames';
|
||||||
|
|
||||||
export {
|
export {
|
||||||
useIsomorphicLayoutEffect,
|
useIsomorphicLayoutEffect,
|
||||||
useDynamicCallback, // TODO rename to useEvent()
|
useEvent,
|
||||||
usePrevious,
|
usePrevious,
|
||||||
ReactContextError,
|
ReactContextError,
|
||||||
} from './utils/reactUtils';
|
} from './utils/reactUtils';
|
||||||
|
|
|
@ -7,7 +7,7 @@
|
||||||
|
|
||||||
import {useEffect} from 'react';
|
import {useEffect} from 'react';
|
||||||
import {useHistory} from '@docusaurus/router';
|
import {useHistory} from '@docusaurus/router';
|
||||||
import {useDynamicCallback} from './reactUtils';
|
import {useEvent} from './reactUtils';
|
||||||
import type {Location, Action} from 'history';
|
import type {Location, Action} from 'history';
|
||||||
|
|
||||||
type HistoryBlockHandler = (location: Location, action: Action) => void | false;
|
type HistoryBlockHandler = (location: Location, action: Action) => void | false;
|
||||||
|
@ -19,7 +19,7 @@ type HistoryBlockHandler = (location: Location, action: Action) => void | false;
|
||||||
*/
|
*/
|
||||||
function useHistoryActionHandler(handler: HistoryBlockHandler): void {
|
function useHistoryActionHandler(handler: HistoryBlockHandler): void {
|
||||||
const history = useHistory();
|
const history = useHistory();
|
||||||
const stableHandler = useDynamicCallback(handler);
|
const stableHandler = useEvent(handler);
|
||||||
useEffect(
|
useEffect(
|
||||||
// See https://github.com/remix-run/history/blob/main/docs/blocking-transitions.md
|
// See https://github.com/remix-run/history/blob/main/docs/blocking-transitions.md
|
||||||
() => history.block((location, action) => stableHandler(location, action)),
|
() => history.block((location, action) => stableHandler(location, action)),
|
||||||
|
|
|
@ -21,18 +21,18 @@ export const useIsomorphicLayoutEffect = ExecutionEnvironment.canUseDOM
|
||||||
: useEffect;
|
: useEffect;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
* Temporary userland implementation until an official hook is implemented
|
||||||
|
* See RFC: https://github.com/reactjs/rfcs/pull/220
|
||||||
|
*
|
||||||
* Permits to transform an unstable callback (like an arrow function provided as
|
* Permits to transform an unstable callback (like an arrow function provided as
|
||||||
* props) to a "stable" callback that is safe to use in a `useEffect` dependency
|
* props) to a "stable" callback that is safe to use in a `useEffect` dependency
|
||||||
* array. Useful to avoid React stale closure problems + avoid useless effect
|
* array. Useful to avoid React stale closure problems + avoid useless effect
|
||||||
* re-executions.
|
* re-executions.
|
||||||
*
|
*
|
||||||
* Workaround until the React team recommends a good solution, see
|
|
||||||
* https://github.com/facebook/react/issues/16956
|
|
||||||
*
|
|
||||||
* This generally works but has some potential drawbacks, such as
|
* This generally works but has some potential drawbacks, such as
|
||||||
* https://github.com/facebook/react/issues/16956#issuecomment-536636418
|
* https://github.com/facebook/react/issues/16956#issuecomment-536636418
|
||||||
*/
|
*/
|
||||||
export function useDynamicCallback<T extends (...args: never[]) => unknown>(
|
export function useEvent<T extends (...args: never[]) => unknown>(
|
||||||
callback: T,
|
callback: T,
|
||||||
): T {
|
): T {
|
||||||
const ref = useRef<T>(callback);
|
const ref = useRef<T>(callback);
|
||||||
|
|
|
@ -16,7 +16,7 @@ import React, {
|
||||||
} from 'react';
|
} from 'react';
|
||||||
import ExecutionEnvironment from '@docusaurus/ExecutionEnvironment';
|
import ExecutionEnvironment from '@docusaurus/ExecutionEnvironment';
|
||||||
import useIsBrowser from '@docusaurus/useIsBrowser';
|
import useIsBrowser from '@docusaurus/useIsBrowser';
|
||||||
import {useDynamicCallback, ReactContextError} from './reactUtils';
|
import {useEvent, ReactContextError} from './reactUtils';
|
||||||
|
|
||||||
type ScrollController = {
|
type ScrollController = {
|
||||||
/** A boolean ref tracking whether scroll events are enabled. */
|
/** A boolean ref tracking whether scroll events are enabled. */
|
||||||
|
@ -104,7 +104,7 @@ export function useScrollPosition(
|
||||||
const {scrollEventsEnabledRef} = useScrollController();
|
const {scrollEventsEnabledRef} = useScrollController();
|
||||||
const lastPositionRef = useRef<ScrollPosition | null>(getScrollPosition());
|
const lastPositionRef = useRef<ScrollPosition | null>(getScrollPosition());
|
||||||
|
|
||||||
const dynamicEffect = useDynamicCallback(effect);
|
const dynamicEffect = useEvent(effect);
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
const handleScroll = () => {
|
const handleScroll = () => {
|
||||||
|
|
|
@ -7,7 +7,7 @@
|
||||||
|
|
||||||
import {useEffect} from 'react';
|
import {useEffect} from 'react';
|
||||||
import {useLocation} from '@docusaurus/router';
|
import {useLocation} from '@docusaurus/router';
|
||||||
import {useDynamicCallback, usePrevious} from './reactUtils';
|
import {useEvent, usePrevious} from './reactUtils';
|
||||||
import type {Location} from 'history';
|
import type {Location} from 'history';
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -24,7 +24,7 @@ export function useLocationChange(
|
||||||
const location = useLocation();
|
const location = useLocation();
|
||||||
const previousLocation = usePrevious(location);
|
const previousLocation = usePrevious(location);
|
||||||
|
|
||||||
const onLocationChangeDynamic = useDynamicCallback(onLocationChange);
|
const onLocationChangeDynamic = useEvent(onLocationChange);
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
if (!previousLocation) {
|
if (!previousLocation) {
|
||||||
|
|
|
@ -20,7 +20,7 @@ import {
|
||||||
HtmlClassNameProvider,
|
HtmlClassNameProvider,
|
||||||
usePluralForm,
|
usePluralForm,
|
||||||
isRegexpStringMatch,
|
isRegexpStringMatch,
|
||||||
useDynamicCallback,
|
useEvent,
|
||||||
} from '@docusaurus/theme-common';
|
} from '@docusaurus/theme-common';
|
||||||
import {
|
import {
|
||||||
useTitleFormatter,
|
useTitleFormatter,
|
||||||
|
@ -316,7 +316,7 @@ function SearchPageContent(): JSX.Element {
|
||||||
description: 'The search page title for empty query',
|
description: 'The search page title for empty query',
|
||||||
});
|
});
|
||||||
|
|
||||||
const makeSearch = useDynamicCallback((page: number = 0) => {
|
const makeSearch = useEvent((page: number = 0) => {
|
||||||
algoliaHelper.addDisjunctiveFacetRefinement('docusaurus_tag', 'default');
|
algoliaHelper.addDisjunctiveFacetRefinement('docusaurus_tag', 'default');
|
||||||
algoliaHelper.addDisjunctiveFacetRefinement('language', currentLocale);
|
algoliaHelper.addDisjunctiveFacetRefinement('language', currentLocale);
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue