Skip to content

Error: Cannot access refs during render when using useRef to store Redux store in Next.js in React 19.* #5291

@kalpovskii

Description

@kalpovskii

Description

When attempting to initialize a Redux store inside a Next.js (https://redux-toolkit.js.org/usage/nextjs#providing-the-store) using useRef to persist the store instance across renders, accessing storeRef.current during render results in error "Error: Cannot access refs during render"

Environment

"react-redux": "^9.2.0"
"react": "19.2.4",
"next": "16.2.4",
"@reduxjs/toolkit": "^2.11.2",

Code Example

'use client'
import { useRef } from 'react'
import { Provider } from 'react-redux'
import { makeStore, AppStore } from '../lib/store'

export default function StoreProvider({
  children,
}: {
  children: React.ReactNode
}) {
  const storeRef = useRef<AppStore>(undefined)

// Error: Cannot access refs during render
  if (!storeRef.current) {
    // Create the store instance the first time this renders
    storeRef.current = makeStore()
  }

// Error: Cannot access refs during render
  return <Provider store={storeRef.current}>{children}</Provider>
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    DocumentationImprovements or additions to documentationRTK-QueryIssues related to Redux-Toolkit-Query

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions