useDefault

Return a default value if the state is null or undefined.

https://www.shaktools.com/shak-hooks
State

API Reference

Installation

terminal
pnpm add @shak-hooks/usehooks
import.ts
import { useDefault } from "@shak-hooks/usehooks";
Source: packages/react/src/useDefault.ts

Signature

signature.ts
export function useDefault<T>(initialValue: T, defaultValue: T)

Parameters

NameTypeOptionalDefault
initialValueTNo-
defaultValueTNo-

Returns

unknown

Implementation

use-default.ts
1import { useState } from "react";
2
3export function useDefault<T>(initialValue: T, defaultValue: T) {
4 const [state, setState] = useState<T | undefined | null>(initialValue);
5
6 return [state === undefined || state === null ? defaultValue : state, setState] as const;
7}

Advertisement

Google Ads

Usage

example.tsx
1import { useDefault } from "@shak-hooks/usehooks";
2
3const result = useDefault(undefined, undefined);
4// result: unknown
5// Use values directly (React state).

Let‘s do great work together

Empowering creators with free, high-performance AI, SEO, and developer tools. Join thousands of users optimizing their workflow with Shak-Tools.

Tools10+ Free
UsersGlobal

Stay in the loop

Join our newsletter for the latest AI tools and updates.

ShakTools
© 2025 Shaktools. All Rights Reserved.Privacy Policy