-
-
Notifications
You must be signed in to change notification settings - Fork 15.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Only warn for unexpected key once per key #1818
Conversation
e016021
to
c9b5c3a
Compare
@@ -34,7 +36,9 @@ function getUnexpectedStateShapeWarningMessage(inputState, reducers, action) { | |||
) | |||
} | |||
|
|||
var unexpectedKeys = Object.keys(inputState).filter(key => !reducers.hasOwnProperty(key)) | |||
var unexpectedKeys = Object.keys(inputState).filter(key => !reducers.hasOwnProperty(key) && !unexpectedKeyCache[key]) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Let’s split this into several lines so they format more nicely?
Looks good to me after fixing the nits. |
c9b5c3a
to
0a3eee0
Compare
@gaearon the nits have been fixed 👍 |
@@ -2,6 +2,8 @@ import { ActionTypes } from './createStore' | |||
import isPlainObject from 'lodash/isPlainObject' | |||
import warning from './utils/warning' | |||
|
|||
const unexpectedKeyCache = {} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Let’s use var
for consistency (we’ll codemod ’em all over later)
0a3eee0
to
2c48641
Compare
@gaearon changes made 👍 |
@@ -237,5 +237,26 @@ describe('Utils', () => { | |||
|
|||
spy.restore() | |||
}) | |||
|
|||
it('only warns for unexpected keys once', () => { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do I undestand correctly that tests now depend on global shared state and run order? This doesn’t look very good.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Right, that should be resolved by using individual cache instances per combineReducers
call
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yep
@gaearon I updated the PR so that it creates and passes a new cache to |
@@ -101,6 +99,7 @@ function assertReducerSanity(reducers) { | |||
*/ | |||
export default function combineReducers(reducers) { | |||
var reducerKeys = Object.keys(reducers) | |||
var unexpectedKeyCache = {} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Probably no need to declare it unless we’re in DEV.
Create key cache in combineReducers Only define unexpectedKeyCache in dev environment
13ddb17
to
2d6316d
Compare
@gaearon updated again. Fourth try's the charm. |
Have you tested this on a real project to check that it behaves as expected? |
I haven't tested it any real project, no. I can run it on some of the example projects and verify the results if you like. |
I tested the real-world example branch and verified it's working as expected. I changed the const rootReducer = combineReducers({
entities,
pagination: (state, action) => {
return pagination(
Object.assign({}, state, {foobar: false}),
action
)
},
errorMessage,
routing
}) With the current release of redux this caused multiple warnings. After running |
👍 |
Create key cache in combineReducers Only define unexpectedKeyCache in dev environment
Resolves #1748
Pretty simple, just keeps a caches and filters keys that have already been warned about.
I had to update one of the related tests as it expected some keys to be warned about multiple times.