react-hook-form: Issue: Cannot add property 0: object is not extensible

Version Number

7.19.1

Codesandbox/Expo snack

Unfortunately, no codesandbox reproduction

Steps to reproduce

I’m really sorry for not having codesandbox repro, as I’m unable to reproduce there. My project is pretty complex and uses useFieldArray heavily. After migrating to 7.19.1 version I’m having such error on …register(‘levels.0.deeperField.0.levelID’) call (actually there are many fields starting from 0 till ~40-50):

Unhandled Runtime Error
TypeError: Cannot add property 0, object is not extensible

These are only stack parts that refer to react-hook-form:

Call Stack
set
node_modules/react-hook-form/dist/index.esm.js (201:0)

handleChange
node_modules/react-hook-form/dist/index.esm.js (1404:0)

Expected behaviour

Please help me to understand for what reason this error may happen. I was not able to find any code in react-hook-form repo that does Object.preventExtensions (maybe in some deps of the project this code exists?) so I’m not sure how to proceed.

What browsers are you seeing the problem on?

Chrome

Relevant log output

Next.js full error stack:

Unhandled Runtime Error
TypeError: Cannot add property 0, object is not extensible

Call Stack
set
node_modules/react-hook-form/dist/index.esm.js (201:0)
handleChange
node_modules/react-hook-form/dist/index.esm.js (1404:0)
handleBlur
node_modules/@mui/material/InputBase/InputBase.js (340:0)
HTMLUnknownElement.callCallback
node_modules/react-dom/cjs/react-dom.development.js (3945:0)
Object.invokeGuardedCallbackDev
node_modules/react-dom/cjs/react-dom.development.js (3994:0)
invokeGuardedCallback
node_modules/react-dom/cjs/react-dom.development.js (4056:0)
invokeGuardedCallbackAndCatchFirstError
node_modules/react-dom/cjs/react-dom.development.js (4070:0)
executeDispatch
node_modules/react-dom/cjs/react-dom.development.js (8243:0)
processDispatchQueueItemsInOrder
node_modules/react-dom/cjs/react-dom.development.js (8275:0)
processDispatchQueue
node_modules/react-dom/cjs/react-dom.development.js (8288:0)
dispatchEventsForPlugins
node_modules/react-dom/cjs/react-dom.development.js (8299:0)
eval
node_modules/react-dom/cjs/react-dom.development.js (8508:0)
batchedEventUpdates$1
node_modules/react-dom/cjs/react-dom.development.js (22396:0)
batchedEventUpdates
node_modules/react-dom/cjs/react-dom.development.js (3745:0)
dispatchEventForPluginEventSystem
node_modules/react-dom/cjs/react-dom.development.js (8507:0)
attemptToDispatchEvent
node_modules/react-dom/cjs/react-dom.development.js (6005:0)
dispatchEvent
node_modules/react-dom/cjs/react-dom.development.js (5924:0)
unstable_runWithPriority
node_modules/scheduler/cjs/scheduler.development.js (468:0)
runWithPriority$1
node_modules/react-dom/cjs/react-dom.development.js (11276:0)
discreteUpdates$1
node_modules/react-dom/cjs/react-dom.development.js (22413:0)
discreteUpdates
node_modules/react-dom/cjs/react-dom.development.js (3756:0)
dispatchDiscreteEvent
node_modules/react-dom/cjs/react-dom.development.js (5889:0)

Code of Conduct

  • I agree to follow this project’s Code of Conduct

About this issue

  • Original URL
  • State: closed
  • Created 3 years ago
  • Comments: 25 (10 by maintainers)

Most upvoted comments

+1 Same issue here. I have classic usage of useFieldArray. When I unmount the container (I have form wizard) i got same app crash…

So the answer is to call replace() with cloned (or something) array object ensuring there’s no preventExtensions object inside - this resolved my personal issue. Otherwise hook form is unable to do it’s job for safely and effectively managing field array data.

@vanyadymousky i will see if i can reproduce this today as well 👍

@bluebill1049 This is an approximate example of my case: https://codesandbox.io/s/suspicious-lovelace-711e1?file=/src/App.tsx And in code sandbox it’s working fine (notice deeperFields is an empty array, but registering deeperFields.0.levelID doesn’t cause an error)

Will try again to repro in codesandbox