Skip to content

jsflor/random-names

Repository files navigation

travis build codecov coverage version downloads MIT License

Random Name Generator

Simplest way to generate random names in JavaScript projects.

Installation

NPM Installation:

    npm install random-names-generator

Usage

JavaScript

// Make sure to include package
const names = require("random-names-generator");

// Load entire array of names
const allNames = names.all;

// Generate a single random name
const randomName = names.random();

React / React Hooks

    // Include useState and useEffect hooks
    import {useState, useEffect} from 'react'

    // Import random-names-generator package into the project
    import names from  'random-names-generator'

    // Setting up a randomName state using the useState hook
    const  [randomName,  setRandomName]  =  useState(null);

    // Generating and setting the random name within useEffect
    useEffect(()  => {
        // Generate a random name
        let name = names.random();

    	// Set the randomName state to the generated random name
        setRandomName(name)
    }, [])

    // Return the randomly generated name
    return(
    	<>
            ...
            <h1>Welcome {randomName}!</h1>
            ...
    	<>
    )