{"record":{"id":"6e83336a0e3b356c","repo":"facebook/react","slug":"takes-an-object-of-state-variables-to-update-or-a","errorCode":null,"errorMessage":"takes an object of state variables to update or a function which returns an object of state variables.","messagePattern":"takes an object of state variables to update or a function which returns an object of state variables\\.","errorType":"exception","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"packages/react/src/ReactBaseClasses.js","lineNumber":62,"sourceCode":" * the future (not synchronously). It will be called with the up to date\n * component arguments (state, props, context). These values can be different\n * from this.* because your function may be called after receiveProps but before\n * shouldComponentUpdate, and this new state, props, and context will not yet be\n * assigned to this.\n *\n * @param {object|function} partialState Next partial state or function to\n *        produce next partial state to be merged with current state.\n * @param {?function} callback Called after state is updated.\n * @final\n * @protected\n */\nComponent.prototype.setState = function (partialState, callback) {\n  if (\n    typeof partialState !== 'object' &&\n    typeof partialState !== 'function' &&\n    partialState != null\n  ) {\n    throw new Error(\n      'takes an object of state variables to update or a ' +\n        'function which returns an object of state variables.',\n    );\n  }\n\n  this.updater.enqueueSetState(this, partialState, callback, 'setState');\n};\n\n/**\n * Forces an update. This should only be invoked when it is known with\n * certainty that we are **not** in a DOM transaction.\n *\n * You may want to call this when you know that some deeper aspect of the\n * component's state has changed but `setState` was not called.\n *\n * This will not invoke `shouldComponentUpdate`, but it will invoke\n * `componentWillUpdate` and `componentDidUpdate`.\n *","sourceCodeStart":44,"sourceCodeEnd":80,"githubUrl":"https://github.com/facebook/react/blob/eafeac097ba51e1eab809c07102126bd5f8e5425/packages/react/src/ReactBaseClasses.js#L44-L80","documentation":"React.Component.prototype.setState validates its first argument: it must be an object of partial state, an updater function, or null/undefined (which bail out). Passing any other primitive (string, number, boolean, symbol, bigint) throws this error before anything is enqueued.","triggerScenarios":"Calling this.setState('loading'), this.setState(true), this.setState(42), or this.setState(Symbol()) inside a class component; often a typo like setState(someStringVariable) intended as a key.","commonSituations":"Confusing setState with a key/value API (e.g. expecting setState('count', 1)); passing an unwrapped variable that is actually a primitive; copy-pasting from hook-style code where setCount(1) is valid.","solutions":["Wrap the update in an object: this.setState({count: 1})","Use an updater function when the next state depends on the previous: this.setState(prev => ({count: prev.count + 1}))","Pass null/undefined to intentionally bail out instead of a meaningless primitive"],"exampleFix":"// before\nthis.setState('loading');\nthis.setState(42);\n\n// after\nthis.setState({status: 'loading'});\nthis.setState({count: 42});","handlingStrategy":"type-guard","validationCode":"// Validate partialState shape before calling setState\nfunction safeSetState(component, partialState, callback) {\n  const ok = partialState == null ||\n    typeof partialState === 'object' ||\n    typeof partialState === 'function';\n  if (!ok) {\n    console.warn('setState ignored non-object/function:', partialState);\n    return;\n  }\n  component.setState(partialState, callback);\n}","typeGuard":"const isSetStateArg = (v: unknown): v is object | Function | null | undefined =>\n  v == null || typeof v === 'object' || typeof v === 'function';","tryCatchPattern":null,"preventionTips":["Enable TypeScript/Flow types on class components so setState(primitive) is rejected at compile time","Remember setState merges objects; write setState({count: n}), never setState('count', n)"],"tags":["class-component","setstate","invalid-argument"],"backgroundTag":"invalid-setstate-argument","analyzedSha":"eafeac097ba51e1eab809c07102126bd5f8e5425","analyzedAt":"2026-08-21T22:01:08.818Z","schemaVersion":2},"datasetVersion":"2026-08-21T23:17:16.201Z"}