Before you can start dispatching async actions, you need to do a few things:
First things first, install json-server globally by running the following command in a terminal window:
npm install --global json-server
Next create a db.json file in the root directory and add the contents shown in the following listing.
{
"tasks": [
{
"id": 1,
"title": "Learn Redux",
"description": "The store, actions, and reducers, oh my!",
"status": "Unstarted"
},
{
"id": 2,
"title": "Peace on Earth",
"description": "No big deal.",
"status": "Unstarted"
},
{
"id": 3,
"title": "Create Facebook for dogs",
"description": "The hottest new social network",
"status": "Completed"
}
]
}
Finally, start the server with the following command. Note that you’re specifying the server to run on port 3001, because the Create React App development server is already running on port 3000:
json-server --watch db.json --port 3001
That’s it! You’ll start to use each of the newly available endpoints as you add more functionality to Parsnip.
With a server configured, you need to install one more package, axios. axios is one of the many options for making AJAX work a bit more pleasant, and it will be your tool for making HTTP requests from the browser throughout the book. Make sure you’re in the parsnip directory and install axios using npm:
npm install axios
If you see the standard npm success output, you’re all set!
Install Redux Thunk using npm from the terminal by running the following command:
npm install redux-thunk
Add the middleware via the applyMiddleware function exported by Redux, as shown in the following listing.
import React from 'react';
import ReactDOM from 'react-dom';
import { createStore, applyMiddleware } from 'redux'; 1
import { Provider } from 'react-redux';
import thunk from 'redux-thunk'; 2
import { tasks } from './reducers';
import App from './App';
import './index.css';
const store = createStore(
tasks,
applyMiddleware(thunk) 3
);
ReactDOM.render(
<Provider store={store}>
<App />
</Provider>,
document.getElementById('root')
)
Before you get off to the races, there’s one more important update to make regarding the Redux DevTools.
Now that we’ve introduced middleware, the old devToolsEnhancer function will no longer do the trick. You’ll import another method from the same DevTools library that can accommodate middleware, called composeWithDevTools, as shown in the following listing.
import { composeWithDevTools } from 'redux-devtools-extension'; 1
...
const store = createStore(
tasks,
composeWithDevTools(applyMiddleware(thunk)) 2
);
Now you’re ready to return to the action! Don’t forget to use the DevTools Chrome extension to see each action come through the system. The API story resumes in chapter 4.
Install lodash, a popular functional programming library, by running the follow command:
npm install lodash
Enzyme is a testing tool that mainly helps with React component testing. Enzyme used to require special configuration to use with Jest, but since Jest version 15, no additional configuration is required. Install Enzyme using npm:
npm install –D enzyme
Included with the book’s source code is a bash script to get you up and running quickly. It requires you to have a few pieces of software installed before you run it:
For OS X and Linux, download the install.sh script, and run the following commands:
chmod +x install.sh ./install.sh
For Windows, download the install.ps1 script and look at the instructions for running PowerShell scripts (https://docs.microsoft.com/powershell/scripting/core-powershell/ise/how-to-write-and-run-scripts-in-the-windows-powershell-ise).
That’s it! The script will clone the book’s repository from GitHub, install json-server (which you’ll use beginning with chapter 4), and start a local development server from a fresh Create React App installation. This should set you up perfectly to begin coding with chapter 2.