Skip to main content
GoodFirstPicks
DashboardIssuesReposLeaderboard

GoodFirstPicks by Leaveitblank © 2026

CreatorRequest a RepoPrivacy PolicyTerms of Service
Bug: useState with class as initial state throws an error | GoodFirstPicks

Bug: useState with class as initial state throws an error

facebook/react 8 comments 26d ago
View on GitHub
lowopenScope: somewhat clearSkill match: maybeReactJavaScriptTypeScript

Why this is a good first issue

The issue is not considered a bug by maintainers and has low priority.

AI Summary

The issue reports confusion when using a class as initial state in `useState`, but maintainers consider this expected behavior. The suggested solution would require changes to how React handles initial state or documentation updates, but there's no consensus on whether this is worth addressing.

Issue Description

Using a JavaScript class as initial state in useState throws an error. This is because mountState check if the initial state is a function by typeof initialState === 'function' and classes are technically functions. However since ES6+, I feel like most developers don't consider classes functions because of the less prototype-inheritance feel since ES6 and the class keyword.

React version: 17.0.2

Steps To Reproduce

  1. Define an ES6 class
  2. Use the class as initialState argument to useState

Code example:

import { useState } from "react";
import ReactDOM from "react-dom";

class A {}

function App() {
  const [cls, setCls] = useState(A);
  return <h2>{cls.name}</h2>;
}

ReactDOM.render(<App />, document.body);

The current behavior

TypeError: Cannot call a class as a function
    at _classCallCheck (eval at z (eval.js:42), <anonymous>:3:11)
    at A (VM302 index.js:19)
    at mountState (react-dom.development.js:10436)
    at Object.useState (react-dom.development.js:10919)
    at useState (react.development.js:954)
    at App (VM302 index.js:25)

Alternatively, the error can be TypeError: Class constructor X cannot be invoked without 'new'.

The expected behavior

As mentioned in the description above, I would

  • either expect classes to work
  • or mention this caveat in the docs.

I guess checking, if the initial state is actually a non-class function, could cause instances of subclasses of functions not to work (depending how the check is done).

GitHub Labels

Status: UnconfirmedType: Discussion

Want to work on this?

Claim this issue to let others know you're working on it. You'll earn 5 points when you complete it!

Risk Flags

  • maintainer feedback suggests this is not a priority
  • potential edge cases with function components
Loading labels...

Details

Points5 pts
Difficultylow
Scopesomewhat clear
Skill Matchmaybe
Test Focusedno