Captain is currently in ALPHA. We are happy to get your feedback

Module: use-seed

Table of contents

Functions

Functions

useSeed

useSeed(initialState?): Object

Custom hook for managing a seed value with additional utility functions.

This hook provides functionality to manage a seed value, generate a new seed, and cache the last used seed. The seed can be initialized with a provided value or defaults to -1, indicating that a new random seed should be generated.

Parameters

NameTypeDefault valueDescription
initialState?number-1The initial state of the seed. If the value is less than 0, a new random seed will be generated.

Returns

Object

  • An object containing the current seed, the last cached seed, and utility functions to manage the seed.

seed - The current seed value.

lastSeed - The last cached seed value, or null if no seed has been cached.

getNewSeed - A function that returns a new seed. If the provided seed is less than 0, a random seed is generated.

cacheSeed - A function to cache the provided seed as the last used seed.

setSeed - A function to set a new seed value.

Example

const { seed, lastSeed, getNewSeed, cacheSeed, setSeed } = useSeed();
 
// Set a new seed
setSeed(1234);
 
// Get a new seed, possibly random
const newSeed = getNewSeed(-1);
 
// Cache the current seed
cacheSeed(seed);
 
// Access the current and last cached seeds
console.log(seed); // 1234
console.log(lastSeed); // 1234 (after caching)

Defined in

react/dist/esm/use-seed/index.js:34