Skip to content
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

Find next todo ID correctly. Fixes #524 #529

Merged
merged 1 commit into from
Aug 15, 2015
Merged

Find next todo ID correctly. Fixes #524 #529

merged 1 commit into from
Aug 15, 2015

Conversation

gaearon
Copy link
Contributor

@gaearon gaearon commented Aug 15, 2015

No description provided.

gaearon added a commit that referenced this pull request Aug 15, 2015
Find next todo ID correctly. Fixes #524
@gaearon gaearon merged commit 78ca80c into master Aug 15, 2015
@gaearon gaearon deleted the fix-todomvc branch August 15, 2015 10:17
@@ -79,7 +79,7 @@ export default function todos(state = initialState, action) {
switch (action.type) {
case ADD_TODO:
return [{
id: state.length,
id: state.reduce((maxId, todo) => Math.max(todo.id, maxId), -1) + 1,

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi @gaearon, Hi @acdlite,

I would like to know that how this particular line of code works.
I am struggling to understand that from where maxId has come and how the ids are getting generated.

Please help me with this. I am new to redux and wish to use it in my current project.

Thanks in advance.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When maxId is not declared then it should have the value as undefined
however its getting the value as null ?

console output:
Math.max(0, undefined)
NaN
Math.max(0, null)
0

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Initially maxId comes from the seed argument to the reduce(fn, seed). In this case the seed is -1. Then, inside the reduce callback, we return another number if it’s bigger. This is how we get the maximum number. Then we just increment it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants