Redux Lab 1: Installation & Configuration
Objectives
- Install Redux and related packages
- Configure Redux
Steps
Install Redux
- Open a
command prompt(Windows) orterminal(Mac). - Change the current directory to
projectpilot. - Run one of the following sets of commands:
npm
npm install redux@4 react-redux@8 redux-devtools-extension redux-thunk@2Yarn
yarn add redux@4 react-redux@8 redux-devtools-extension redux-thunk@2 - After the installs finish, open the
\package.jsonfile and quickly verify the packages were added to thedependenciesanddevDependencies.
Configure Redux
Create the following file and configure Redux.
src\state.tsimport { createStore, applyMiddleware } from 'redux';
import ReduxThunk from 'redux-thunk';
import { composeWithDevTools } from 'redux-devtools-extension';
import { combineReducers } from 'redux';
const reducer = combineReducers({});
export default function configureStore(preloadedState: any) {
const middlewares = [ReduxThunk];
const middlewareEnhancer = applyMiddleware(...middlewares);
//Thunk is middleware
//DevTools is an enhancer (actually changes Redux)
//applyMiddleware wraps middleware and returns an enhancer
// to use only thunk middleware
// const enhancer = compose(middlewareEnhancer);
//to use thunk & devTools
const enhancer = composeWithDevTools(middlewareEnhancer);
const store = createStore(reducer, preloadedState, enhancer);
return store;
}
export interface AppState {}
export const initialAppState: AppState = {};
export const store = configureStore(initialAppState);Verify the application compiles.