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

Code error in the example explained in the API doc for "combineReducers" ? #1616

Closed
parallelthought opened this issue Apr 14, 2016 · 3 comments

Comments

@parallelthought
Copy link

In the example described as part of API doc of "combineReducers", at the time of the store creation, "reducer" as a parameter is passed on to createStore in the file "app.js" (let store = createStore(reducer)).

However in "reducers/index.js", the function name is "combineReducers" and not "reducer" (export default combineReducers({..})).

So shouldn't the parameter pass on at the creation of the store be "combineReducers" (Eg: let store = createStore(combineReducers)) ??

@markerikson
Copy link
Contributor

I'd say no. If you think about it, Redux only has a single reducer function. The fact that this example is using combineReducers to create that function is an implementation detail. As far as app.js is concerned, it doesn't know or care what's going on inside that function, how we created it, or what it's doing.

Also, note that when reducers/index.js does export default combineReducers({}), it's not actually creating a thing named "combineReducers". It's saying "take the result of this function call, whatever it is, and make it the default export." In fact, technically, that output does not even have any real variable name associated with it.

Finally, note that when you do export default, whatever file imports that default export can give any name it wants to the local variable, same as how a function definition can name a parameter something different from whatever variable name was used at the call site. Examples:

function someFunction(a, fred, randomVariableName) {}

const firstVariable = 1;
const theMeaningOfLife = 42;
const aTotallyUselessName = "Whatever";

someFunction(firstVariable, theMeaningOfLife, aTotallyUselessName);

Note how the variable names I define outside the function don't have to have any correspondence with the parameter names inside the function.

Similarly, when exporting:

// fileA.js
export default const aThing = 42;

// fileB.js
import iCanGiveThisAnyNameIWant from "fileA";

@gaearon
Copy link
Contributor

gaearon commented Apr 14, 2016

@markerikson Thank you for these great answers.

@parallelthought
Copy link
Author

Thank you very much @markerikson.

I'm learning Redux to be used with JQuery, minus the knowledge of React. Your answer now helps me to better understand the examples with the React code.

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

No branches or pull requests

3 participants