Skip to content
masterclassBuddyTech Buzz5 fresh blogs · 5:00 AM IST

Redux Toolkit · Function

nanoid

nanoid is a Redux Toolkit utility that generates a random, non-cryptographically-secure ID string. createAsyncThunk uses it by default for request IDs, and applications can call it directly for other ordinary identifiers.

How it works

nanoid generates a random identifier string using Redux Toolkit's inlined nonsecure implementation. It is available as a direct export and is also used internally by createAsyncThunk for request IDs.

Syntax

nanoid()

Returns

A non-cryptographically-secure random ID string.

Key points

  • Redux Toolkit includes its own inlined nonsecure implementation rather than requiring a separate nanoid package.
  • The function produces a random string identifier, but it does not provide cryptographic security.
  • createAsyncThunk automatically uses nanoid to assign request IDs to its async requests.
  • You can also use nanoid directly for application identifiers outside asynchronous thunk requests.
  • Import nanoid from the top-level Redux Toolkit package before calling it.

Common mistakes

  • Do not use nanoid when an identifier must provide cryptographic security.

Example: Generate an ID

import { nanoid } from '@reduxjs/toolkit'

const id = nanoid()
console.log(id)

Calling nanoid returns a newly generated random ID string.

When to use it

Temporary entity IDs

Creating client-side entities before they are persisted.

Request correlation

Identifying asynchronous thunk executions.

Action payload IDs

Adding unique identifiers to action payloads.

Interview questions

QWhat does Redux Toolkit's nanoid function do?

nanoid is a Redux Toolkit export that generates a random ID string. Redux Toolkit uses an inlined copy of nanoid/nonsecure, so the result is not cryptographically secure.

Follow-up: Why might createAsyncThunk use it? It needs a unique request ID to identify each dispatched async request, and nanoid provides that by default.

QHow is nanoid called and what does it return?

Call nanoid with no arguments, for example nanoid(). It returns the generated string directly, so it can be assigned to an entity, action payload, or other application value.

Follow-up: How would you use it when creating an action payload? Import nanoid from @reduxjs/toolkit and invoke it while constructing the payload's ID field.

QWhen is Redux Toolkit's nanoid useful?

Redux Toolkit documents nanoid as potentially useful beyond createAsyncThunk, while specifically noting that createAsyncThunk uses it by default for request IDs. It is therefore suitable for ordinary random identifiers where cryptographic security is unnecessary.

Follow-up: What should replace it for security-sensitive tokens? Use a cryptographically secure identifier generator rather than Redux Toolkit's nonsecure implementation.

  • createAsyncThunk
  • createAction
  • createSlice

Source: Other Exports. Checked 2026-10-15.