-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(ui-hooks): update useSafeMemo and useUpdatedRef (#2309)
also update 'react-hooks/exhaustive-deps' ESLint rule
- Loading branch information
Showing
14 changed files
with
106 additions
and
82 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
/** | ||
* Copyright (c) 2025, RTE (https://www.rte-france.com) | ||
* | ||
* See AUTHORS.txt | ||
* | ||
* This Source Code Form is subject to the terms of the Mozilla Public | ||
* License, v. 2.0. If a copy of the MPL was not distributed with this | ||
* file, You can obtain one at http://mozilla.org/MPL/2.0/. | ||
* | ||
* SPDX-License-Identifier: MPL-2.0 | ||
* | ||
* This file is part of the Antares project. | ||
*/ | ||
|
||
import { useState } from "react"; | ||
import { useUpdateEffect } from "react-use"; | ||
|
||
/** | ||
* Hook that returns a memoized value with semantic guarantee. | ||
* | ||
* Semantic guarantee is not provided by `useMemo`, which is solely used | ||
* for performance optimization (cf. https://react.dev/reference/react/useMemo#caveats). | ||
* | ||
* @param factory - A function that returns the value to memoize. | ||
* @param deps - Dependencies that trigger the memoization. | ||
* @returns The memoized value. | ||
*/ | ||
function useSafeMemo<T>(factory: () => T, deps: React.DependencyList): T { | ||
const [state, setState] = useState(factory); | ||
|
||
useUpdateEffect(() => { | ||
setState(factory); | ||
}, deps); | ||
|
||
return state; | ||
} | ||
|
||
export default useSafeMemo; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
/** | ||
* Copyright (c) 2025, RTE (https://www.rte-france.com) | ||
* | ||
* See AUTHORS.txt | ||
* | ||
* This Source Code Form is subject to the terms of the Mozilla Public | ||
* License, v. 2.0. If a copy of the MPL was not distributed with this | ||
* file, You can obtain one at http://mozilla.org/MPL/2.0/. | ||
* | ||
* SPDX-License-Identifier: MPL-2.0 | ||
* | ||
* This file is part of the Antares project. | ||
*/ | ||
|
||
import { useLayoutEffect, useRef } from "react"; | ||
|
||
/** | ||
* Hook that returns a mutable ref that automatically updates its value. | ||
* | ||
* @param value - The value to store in the ref. | ||
* @returns The mutable ref. | ||
*/ | ||
function useUpdatedRef<T>(value: T): React.MutableRefObject<T> { | ||
const ref = useRef(value); | ||
|
||
// `useLayoutEffect` runs before `useEffect`. So `useLayoutEffect` is used to make sure | ||
// the value is up-to-date before any other code runs. | ||
useLayoutEffect(() => { | ||
ref.current = value; | ||
}); | ||
|
||
return ref; | ||
} | ||
|
||
export default useUpdatedRef; |