useCounter

Manage a counter state.

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

API Reference

Installation

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

Signature

signature.ts
export function useCounter(initialValue: number = 0, options: CounterOptions = {}): UseCounterReturn

Parameters

NameTypeOptionalDefault
initialValuenumberYes0
optionsCounterOptionsYes{}

Returns

UseCounterReturn

Exported Types

types.ts
1export interface CounterActions {
2 inc: (delta?: number) => void;
3 dec: (delta?: number) => void;
4 get: () => number;
5 set: (value: number) => void;
6 reset: (value?: number) => void;
7}
8
9export type UseCounterReturn = [
10 number,
11 CounterActions
12];

Implementation

use-counter.ts
1import { useState, useCallback } from "react";
2
3export function useCounter(initialValue: number = 0) {
4 const [count, setCount] = useState(initialValue);
5
6 const increment = useCallback(() => setCount((x) => x + 1), []);
7 const decrement = useCallback(() => setCount((x) => x - 1), []);
8 const reset = useCallback(() => setCount(initialValue), [initialValue]);
9 const set = useCallback((value: number) => setCount(value), []);
10
11 return { count, increment, decrement, reset, set };
12}

Advertisement

Google Ads

Usage

example.tsx
1import { useCounter } from "@shak-hooks/usehooks";
2
3const result = useCounter(undefined, undefined);
4// result: UseCounterReturn
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