You defined the const ADD, why not use that? I don’t think that breaks your code, but that is best practice.
The instructions say:
You’ll need to pass a message to this action creator and include the message in the returned action.
You didn’t do that, the message needs to be passed on the action.
return {
...state,
message: action.message
}
What is the shape of state? Look at the initial state (the default in the reducer parameters) and look at this. This implies that it is an object with a “message” property. That is not what the reducer state is supposed to be.
You didn’t do this from the instructions:
Finally, create your Redux store and pass it the reducer.
I don’t understand well what you mean here since the instruction says to define an action creator addMessage() which creates the action to add a message.
[quote=“kevinSmith, post:2, topic:549420”]
What is the shape of state? Look at the initial state (the default in the reducer parameters) and look at this. This implies that it is an object with a “message” property. That is not what the reducer state is supposed to be.[/quote]
function messageReducer(state = , action){
switch(action.type) {
case ADD:
return […state, action.message]
default:
return state
}
}
}
}
I tried this since the initial state is an array.
const store = createStore(messageReducer)
Sorry I forgot this part. But there is still an issue here because it says ReferenceError: Can’t find variable: createStore
will add a property “text” that will have the string “message”. Is that what you want? Look at how your reducer is using it:
return […state, action.message]
It looks like the action should have a property “message”. What is stored in there?
I tried this since the initial state is an array.
Yeah, that looks right to me. The reducer state is an array of strings, which are our messages.
Sorry I forgot this part. But there is still an issue here because it says ReferenceError: Can’t find variable: createStore
It’s hard to see because we are in the little FCC IDE but I think that is available on the module Redux, so you would access it as Redux.createStore. True, it could have been set up so createStore was a standalone function, but it wasn’t in this case.
is better. It has the right property name, but what you are storing in there is the string literal “message” - the message will be the text “message”. We don’t want that, we want the string that was passed into the action creator. For example, if I had an action creator to store a record, it might look like
const storeRecord = record => {
return {
type: STORE_RECORD,
record: record
}
}
Or, using ES6 shorthand I could do:
const storeRecord = record => {
return {
type: STORE_RECORD,
record
}
}
since the property and the variable have the same name.
Am I the only one to spend hours on React Redux and JS algorithm every single challenge ?
Yes, you are the only one confused by this stuff. Oh, no, wait, the opposite of that. Most people get confused by this stuff, I know I did. Algorithms are hard. But the more you do, the easier they get. And redux is weird. It is a very different way of thinking. It seems unnecessarily complex and when you are learning it it is not clear why you need it or why it has to be this weird or even how it works. But I assure you, if you do React, you want to know redux. (And there are applications outside React.) It seems really weird at first but it is actually really powerful and helps you right much cleaner code.
Is there a better way to write this ?
Within the confines of redux?
Most people would write the action creator this way:
The other thing I’d say is to format your code as you go. I know it’s a pain but it becomes second nature and it really, really saves time. We spend a lot more time reading code than writing it, so making it easier it to read makes our jobs easier, especially when you have to read through 1000 lines of code.
But other than that, that’s basic redux. That’s how you write it. It might be split into different files, but that’s the idea.