• Custom React hook that updates the state only once when the current value matches the target value.

    This hook is useful for setting a state based on a condition that only needs to be fulfilled once. When the currentValue equals the targetValue, it sets the state to the targetValue.

    Example

    const MyComponent = () => {
    const [inputValue, setInputValue] = useState('');
    const state = useOnceProp(inputValue, 'submit');

    useEffect(() => {
    if (state === 'submit') {
    console.log('State updated to submit');
    }
    }, [state]);

    return (
    <input
    type="text"
    value={inputValue}
    onChange={(e) => setInputValue(e.target.value)}
    />
    );
    };

    Type Parameters

    • T

    Parameters

    • currentValue: T

      The current value to be compared.

    • targetValue: T

      The value to match against the current value.

    Returns T