reduxjs/redux

Object.constructor is used as state for reducer named "constructor"

boredcity opened this issue ยท 5 comments

What is the current behavior?

if combineReducers is passed an object containing constructor key, initialState is erased and Object.constructor is used as state instead.

Steps to Reproduce

https://codesandbox.io/s/react-redux-hooks-forked-jf8xe?file=/src/store.js

What is the expected behavior?

making constructor behave as any other key would be nice, but if there are some technical limitations even warning (both during combineReducers exectution and docs) would help a lot ๐Ÿ‘

Environment Details

"react-redux": "^7.2.5",
"redux": "^4.1.1",

...and I'd love to help (make a pull request) on this issue if you do consider this a problem worth fixing ๐Ÿ‘

I'm legitimately curious: why would you actually have a key named constructor in your state in the first place?

I absolutely can understand the curiosity ๐Ÿ˜„

I'm actually teaching a react/redux course and as a part of students' graduation project (website for a fictional fastfood restaurant) they have to build an app with, among other things, drag-and-drop burger builder ("choose your own ingredients" kind of deal).

one of the students chose this naming since "constructor" is a valid english translation from russian for this section ๐Ÿคทโ€โ™‚๏ธ

I do understand that this is a very niche problem, but it would make sense to at least mention that one naming limitation in docs since newcomers can get stuck on this ๐Ÿ‘€

Huh. Yeah, I'm not sure there's anything we should or even can do about this at the code level.

I guess if you want to file a PR to add a note to the combineReducers docs page we can add that, although even there this seems like it's really a JS-level issue and not something specific to Redux itself.

I'm going to close this because I feel it's too much of an edge case for us to do anything about. The overall answer here is that using fields named constructor or prototype are potential problems for JS in general, not just Redux.