learn-co-curriculum / combine-reducers Goto Github PK
View Code? Open in Web Editor NEWLicense: Other
License: Other
case "ADD_AUTHOR": return { ...state, authors: [...state.authors, action.author] };
Here, you are copying over the properties that are not changing(i,.e., books) but the spread operator does not work in this case. The books property will still have a reference to the array exists in the old state. You could do something like this:
case "ADD_AUTHOR": return { ...state, books: [...state.books], authors: [...state.authors, action.author] };
Hi there,
Just wanted to pass this along, I noticed in this code along its quite difficult to understand where these code changes are going and being implemented when the snippets themselves don't have any of the file paths at the top for reference.
I hope this could be useful to cohort members in the future! :)
Sincerely,
MD
This lab feels lazily done - There should be a code repo for students to follow along with. Why not also update the lab so that the code doesn't "have a bug in it."
Hello! In the initial combined reducer, I think the following:
case "REMOVE_AUTHOR":
idx = state.authors.indexOf(action.id);
return {
...state,
authors: [state.authors.slice(0, idx), state.authors.slice(idx + 1)]
};
should be:
case "REMOVE_AUTHOR":
idx = state.authors.indexOf(action.id);
return {
...state,
authors: [...state.authors.slice(0, idx), ...state.authors.slice(idx + 1)]
};
else the function returns an array for authors that consists of 2 arrays (one for the first slice and one for the second) instead of a single array.
Same goes for the REMOVE_BOOK code. Thanks!
Dispatch code in paragraph before last code snippet is:
store.dispatch({ action: 'ADD_BOOK', book: { title: 'huck finn', **authorName**: 'Mark Twain' } });
then in last code snippet, authorsReducer/case 'ADD_BOOK'
let existingAuthor = state.filter(author => author.name === **action.author.name**)
if(!existingAuthor) {
return state.concat(action.author);
} else {
return state
}```
...but only has a readme.md
@Lukeghenco
In many examples a pattern like this is used:
case "ADD_BOOK":
return Object.assign(state, {
books: state.books.concat(action.book)
});
However, this modifies the existing state
object which is bad practice. Object.assign
should take an empty object as its first parameter (e.g. Object.assign({}, state, { ...
) so that existing state remains un-mutated.
Replace the first asterisk with an underscore, and the second asterisked word should just be in italics, with no preceding underscore:
"In the new "ADDBOOK" case, we're checking to see if an authorName matches with the name dispatches from the BookInput component. If the name already exists, state is returned unchanged. If the name is not present, it is added to the author array. Use the example above to modify the manageAuthorsAndBooks reducer and you can see the effect. We have two separate forms, one for adding just authors, and one that adds books _and authors."
Thank you.
Code snippets for booksReducer
and authorsReducer
should mention that they are still in the same manageAuthorsAndBooks
.js file. There were no ellipses in the code blocks so I assumed that they were newly created files.
The readme default reducer includes author in book actions and book in author actions for immutability. This is not reflected in the actual code in the lab.
During the rewrite of the the manageAuthorsAndBooks
file, when adding the "ADD_BOOK" case to the authorsReducer
, they make use of the uuid()
function even though it was never introduced or imported.
This can be solved by adding this import:
import uuid from 'uuid'
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.