npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2026 – Pkg Stats / Ryan Hefner

@utilityjs/use-init-once

v2.0.0

Published

A React hook that holds a lazy-initialized value for a component's lifecycle.

Readme

UtilityJS | useInitOnce

A React hook that holds a lazy-initialized value for a component's lifecycle.

Features

  • Lazy Initialization: Initialize values only when needed, not on every render
  • Performance Optimization: Avoid expensive calculations on re-renders
  • Stable References: Maintain the same reference across component re-renders
  • TypeScript Support: Full type safety with generic support
  • Simple API: Easy-to-use hook with minimal overhead

Installation

npm install @utilityjs/use-init-once

or

pnpm add @utilityjs/use-init-once

Usage

Basic Usage

import { useInitOnce } from "@utilityjs/use-init-once";

function ExpensiveComponent() {
  // This expensive calculation only runs once
  const expensiveValue = useInitOnce(() => {
    console.log("This only runs once!");
    return performExpensiveCalculation();
  });

  return <div>Result: {expensiveValue}</div>;
}

function performExpensiveCalculation() {
  // Simulate expensive operation
  let result = 0;
  for (let i = 0; i < 1000000; i++) {
    result += Math.random();
  }
  return result;
}

Unique ID Generation

import { useInitOnce } from "@utilityjs/use-init-once";

function ComponentWithUniqueId() {
  // Generate a unique ID that persists across re-renders
  const id = useInitOnce(
    () => `component-${Math.random().toString(36).substr(2, 9)}`,
  );

  return <div id={id}>Component with stable ID: {id}</div>;
}

Complex Object Initialization

import { useInitOnce } from "@utilityjs/use-init-once";

interface Config {
  apiUrl: string;
  timeout: number;
  retries: number;
}

function ApiComponent() {
  // Initialize complex configuration object once
  const config = useInitOnce<Config>(() => ({
    apiUrl: process.env.REACT_APP_API_URL || "https://api.example.com",
    timeout: 5000,
    retries: 3,
  }));

  const fetchData = async () => {
    // Use the stable config object
    const response = await fetch(config.apiUrl, {
      timeout: config.timeout,
    });
    return response.json();
  };

  return (
    <div>
      <p>API URL: {config.apiUrl}</p>
      <button onClick={fetchData}>Fetch Data</button>
    </div>
  );
}

Date/Time Initialization

import { useInitOnce } from "@utilityjs/use-init-once";

function TimestampComponent() {
  // Capture the component's creation time
  const createdAt = useInitOnce(() => new Date());

  // Generate a session ID that persists
  const sessionId = useInitOnce(
    () => `session-${Date.now()}-${Math.random().toString(36).substr(2, 9)}`,
  );

  return (
    <div>
      <p>Component created at: {createdAt.toLocaleString()}</p>
      <p>Session ID: {sessionId}</p>
    </div>
  );
}

Class Instance Initialization

import { useInitOnce } from "@utilityjs/use-init-once";

class DataProcessor {
  private cache = new Map();

  process(data: string): string {
    if (this.cache.has(data)) {
      return this.cache.get(data);
    }

    const result = data.toUpperCase().split("").reverse().join("");
    this.cache.set(data, result);
    return result;
  }
}

function ProcessorComponent() {
  // Initialize class instance once
  const processor = useInitOnce(() => new DataProcessor());

  const [input, setInput] = useState("");
  const [output, setOutput] = useState("");

  const handleProcess = () => {
    setOutput(processor.process(input));
  };

  return (
    <div>
      <input
        value={input}
        onChange={e => setInput(e.target.value)}
        placeholder="Enter text to process"
      />
      <button onClick={handleProcess}>Process</button>
      <p>Output: {output}</p>
    </div>
  );
}

Comparison with useMemo

import { useInitOnce } from "@utilityjs/use-init-once";
import { useMemo } from "react";

function ComparisonExample({ dependency }: { dependency: string }) {
  // useMemo: Re-runs when dependency changes
  const memoizedValue = useMemo(() => {
    console.log("useMemo: Recalculating...");
    return `Processed: ${dependency}`;
  }, [dependency]);

  // useInitOnce: Only runs once, ignores dependency changes
  const initOnceValue = useInitOnce(() => {
    console.log("useInitOnce: Calculating once...");
    return `Initial: ${dependency}`;
  });

  return (
    <div>
      <p>Memoized: {memoizedValue}</p>
      <p>Init Once: {initOnceValue}</p>
    </div>
  );
}

API

useInitOnce<T>(initFactory: () => T): T

Initializes a value only once during the component's lifecycle.

Parameters

  • initFactory (() => T): A function that returns the initial value. This function is called only once during the component's first render.

Returns

  • T: The initialized value that persists across re-renders.

Type Parameters

  • T: The type of the value to be initialized.

Example

const value = useInitOnce(() => expensiveCalculation());

Use Cases

  • Performance Optimization: Avoid expensive calculations on every render
  • Unique ID Generation: Create stable IDs that don't change on re-render
  • Class Instance Creation: Initialize class instances once per component
  • Configuration Objects: Create configuration objects that persist
  • Date/Time Snapshots: Capture timestamps at component creation
  • Cache Initialization: Set up caches or data structures once

Contributing

Read the contributing guide to learn about our development process, how to propose bug fixes and improvements, and how to build and test your changes.

License

This project is licensed under the terms of the MIT license.