@trellixio/roaster-coffee
Version:
Beans' product component library
40 lines (39 loc) • 1.2 kB
JavaScript
import * as React from 'react';
/**
* A reducer function that returns the next value in the range of [0, 1000000) when given a value in that range.
* This reducer is used to update the value passed to `useReducer` and trigger a re-render.
*
* @param value - The current value of the reducer.
* @returns The next value in the range of [0, 1000000).
*/
const reducer = (value) => (value + 1) % 1000000;
/**
* A custom React hook that returns a function to forcefully update a component.
* This hook utilizes the `useReducer` hook to update a value that is not used, which forces a re-render of the component.
*
* @returns A function that can be called to force a re-render of the component.
*
* @example
*```tsx
* function MyComponent() {
* const forceUpdate = useForceUpdate();
* const [count, setCount] = useState(0);
*
* const handleClick = () => {
* setCount(count + 1);
* forceUpdate();
* };
*
* return (
* <div>
* <p>Count: {count}</p>
* <button onClick={handleClick}>Increment</button>
* </div>
* );
* }
* ```
*/
export function useForceUpdate() {
const [, update] = React.useReducer(reducer, 0);
return update;
}