All files / src/hooks useChanged.ts

100% Statements 6/6
100% Branches 2/2
100% Functions 1/1
100% Lines 5/5

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21                            5x 6684x 6684x 499x 499x    
// SPDX-License-Identifier: GPL-3.0-only
// Copyright (C) 2026 Max Boksem. See NOTICE for additional terms under GPLv3 section 7.
 
import { useState } from "react";
 
/**
 * Whether `value` differs from the one the last render saw. The first render
 * doesn't count as a change. Values are compared with `Object.is`.
 *
 * On the render that returns true, the caller can reset state that belonged
 * to the old value. React then renders again straight away, before anything
 * reaches the screen, where a reset in an effect would show the old state for
 * a frame and cost another commit.
 */
export const useChanged = (value: unknown): boolean => {
	const [last, setLast] = useState(value);
	if (Object.is(last, value)) return false;
	setLast(value);
	return true;
};