I'd like to control the display of messages in this message component using Redux. Let's start by creating a new reducer for it. In my reducer's directory, I'm going to create a new file. I'm going to call it messages.js.
I'd like my default export from this file to be my reducer function, so I'm going to export default function. This function's going to take in two arguments just like any reducer. It's going to take in my state and it's going to take in an action.
In this case, our case is just going to be our message, so we're going to default our state value to an empty string. Then inside my reducer function, I'm going to use a switch statement.
I'm going to switch based on my action's type and my default case. I'm just going to return the state without any modification.
I'm also going to add a case for an action type which we're going to call message_shell. You notice I'm using a constant here rather than a string. We'll add that constant value in a second. All we're going to do here is return our action.payload. Payload is going to be the actual message that we want to show.
Then up at the top of the file here, I'm going to define a constant which I'll call message shell. That's just going to be the string message_shell.
We also want to make it easy to create one of these action objects, so I'm going to export a constant which I'm going to call show message. Show message is going to be an action creator, so it's going to accept our message string.
It's going to return an action object which is going to have a type. That type is going to be message_shell. It's going to have a payload which is going to be our message string.
Now we have our reducer set up to show a message, and we have an action creator to do that. So I'm going to save this file. Then back in the source directory, I'm going to open up store.js.
We need to refactor store.js a little bit. I'm going to come down here to where I'm importing reducer from reducers/todo. I'm going to give this a more appropriate name. I'm going to call this to do reducer.
Then I'm also going to import message reducer from reducer/messages. Now we're pulling in two reducers.
But you'll notice that create store only accepts a single reducer as an argument. We can fix this by also importing combine reducers from Redux.
Now we can come down here, and I can declare a constant, which I'll call reducer. This is going to be a call to combine reducers.
Combine reducers is going to take an object. In this object, I'm going to map keys to reducer. I'm going to map to do to the to do reducer that I imported, and I'm going to map message to the message reducer. Essentially what this does is create named spaces within the state, and each named space is going to correspond to the subset of the overall application state that a particular reducer is focused on.
Everything related to my to dos that's handled by the to do reducer will be accessible through state.to do. Then my message, which is handled by the message reducer, will be accessible through state.message.
Their store configuration updated, let's save this. We'll see when the browser reloads, we've broken our app. The problem is that we didn't have this to do property between state and our individual to do items, like in this case, to dos, so we need to refactor some of our existing components.
I'm just going to go into my components directory. I'm going to open up to do form.js. I'm going to scroll down to where I'm calling connect. I'm just going to update this call to state to state.todo.current to do. I can save that.
My application is still broken because I also need to go into to do list, scroll to the bottom, and update where I'm mapping my state to props here to add that to do named space. Now our application's back to a working state.
Let's open up message.js. Let's connect this component. In order to do that, I'm going to import connect. That's going to come from React Redux.
Then down at the bottom of my file where I'm exporting default message, I'm going to export default. I'm going to use a call to connect. Then I'm going to pass message into the resulting function.
Connect is going to take the map state to props argument. I'm going to pass out a function that accepts state and returns my props which in this case is going to be a simple object that just has a message property. That's going to map to state.message. When I save that, the app reloads, and everything's still working.
Now that the message component is connected to Redux, I'm going to go and find todo.js where we're defining all of our action creators for the to do reducer. I want to dispatch my show message action creator here.
I'm going to start by importing show message. That's going to come from our messages file. With that imported, I'm going to scroll down to my saved to do function where I'm dispatching an asynchronous action to create a new to do.
What I'd like to do here is call dispatch. I'm going to pass it my show message action creator. I'm just going to show the message saving to do while it's in the act of saving the to do. I'm going to save this.
When the browser reloads, I'm going to add a new to do. I'm going to save that. We should see our message being displayed above the input.