Instructor: Inside of this createReactApp, I've installed Redux, as well as Flow. Then also made this Flow script so that as we add our Flow types, we can run this script in our terminal, and make sure everything's working correctly.
Inside of our app.js file, I have created this counter component, where if we look in the UI, it simply just adds and subtracts. Now, I've already gone through and integrated Redux. These two buttons, when clicked on, are firing Redux action creators. Then we're using our reducer state to show the current number.
Both our state and Redux action creators are being used with the React Redux connect function, and then exported out. Inside of this index.js file, we've imported this counter component, and then defined our reducer.
You can see that we have an initial state of zero, and have two methods that handle the increment and decrement of our state. Finally, we create our store, add our counter component, and connect it all up with ReactDOM.
As soon as we add our Flow tag at the top of these pages, then save them out, grab our terminal, we can see instantly that Flow has found some errors with our code. Let's go ahead and work through these.
We know that our counter component relies on three things, our reducer state, and these two Redux action creators. Let's first define these types, and then we can integrate them into our React component and Redux concepts.
Up here at the top, let's do export type increment equals an object type increment. Then export type decrement equals an object type decrement. Then export type action is an increment or a decrement. Then export type state equals an object count as a number.
With these action types defined, whenever they're used, whether it be as a prop inside of our counter component within the connect function or inside of our reducer, the type will be consistent throughout.
Same goes for our general action and state types. As we reference state throughout the files, and within our components, it will be kept consistent throughout. Now, let's create our props type. We'll do decrement equals a function that returns a decrement action, and then increment, which is a function that returns an increment action, as well as our count as a number.
Then we can pass this through as a type argument to our component. Now that we've type checked our component props, let's go ahead and type check our connect function. Inside of this actions object, we want to make sure that this increment property returns our increment action.
We'll do that by adding the increment type, and then on the other side of the decrement, we'll do the same thing. Now, inside of our mapStateToProps, we want to make sure that we actually do return our state in the correct type format.
We do that by adding parentheses around this, and making sure that we return our state type. Next, we're going to import our connector type, which we get from React Redux, and then use it on our const connector variable.
Connector has two type arguments. We're going to do an object and props. This connector type does all the heavy type checking for us. The first type argument is for any owned props, and the second is for our component props type.
Now that we've gone through and added types to our app.js page, let's save this out, check our terminal, and see how it's looking so far. It looks like we still have some errors and they are coming from the index page.
Let's go and add types to our index page now. Inside of our index.js file, we want to import our increment, decrement action and state types that we've defined in our app file. Now, let's go and add our state type throughout this file.
We'll add it to our initial state, our handleIncrement function, as well as the handleDecrement function, and set up of our reducer, and finish up with our create store variable. Next up, we'll add our action type to our reducer.
Finally, we'll finish up by adding our individual increment and decrement types to our reducer methods. Perfect. To recap, we've defined our types inside of our app file here, and then we integrated them into our React component, and our connect method.
Then inside of our index.js file, we've imported these types, and then added them to every instance our actions and state are referenced. This includes inside of our reducer and our store instance. Now, let's check our terminal, and see if there are any errors left.
It looks like we still do have an error, and it deals with the element type inside of our ReactDOM method. Let's go ahead and fix that right now. Flow makes this error because it cannot find this element here.
In order to fix this, we could rearrange our code between our other files, or we can go through our Flow config, and add an ignore option. Now, with this suppress comment option that we've got here, we can add this commented out Flow ignore to our index file. It will ignore the next line.
If we save that and open up our terminal again, run our Flow script. As it parses the new logs, we can see if we've covered all of our errors, and it looks like we have.