r/reactjs • u/swyx • Jun 03 '18
Beginner's Thread / Easy Question (June 2018)
Hello! just helping out /u/acemarke to post a beginner's thread for June! we had over 270 comments in last month's thread! If you didn't get a response there, please ask again here! You are guaranteed a response here!
Soo... Got questions about React or anything else in its ecosystem? Stuck making progress on your app? Ask away! We’re a friendly bunch. No question is too simple.
The Reactiflux chat channels on Discord are another great place to ask for help as well.
Pre-empting the most common question: how to get started learning react?
You might want to look through /u/acemarke's suggested resources for learning React and his React/Redux links list. Also check out http://kcd.im/beginner-react.
View all comments
Show parent comment
1
u/swyx Jun 27 '18
/u/acemarke is our redux guru round these parts and probably maintains this example so he'll chime in at some point
i'll give it a small crack:
intersting. this isn't a "normal" top level reducer. you can see in line 31 it takes
state[productId]and performs the action on that. so really what its doing is a "selector" - you have an array of id's instate, and you only want to perform an action on ONE of the things in state, so you pass an action with anaction.productIdtobyId, andbyIdhelps you forward it to theproducts(state,action)"sub-reducer". make sense? im making up the names for all these, code is code but its sometimes handy to name what things are doing.umm.. i dont think so. see above.
this example does use the new-ish object initializer syntax - which is what might be confusing you a bit.
so i think that comes from the api. see
/api/products.json. i agree that this example is extremely confusing from this point of view.what do you mean it is a completely undefined function? it's not a function at all. it looks at
statefor a key determined byproductId, and if theres a number it goes with that number. Ifstatedoesn't have a key for thatproductId, it just goes with0. either way, it adds 1 to that as that is the action we are performing,ADD_TO_CART.hope that helps. stick with it, i know it feels like a lot but you get good at reading functional code.