Simplest way to generate random names in JavaScript projects.
NPM Installation:
npm install random-names-generator
// 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();
// 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>
...
<>
)