Introduction
In this article, we will create a demo application using React, Redux, and web API. We will learn the basics of Redux and how to use Redux in a React application. Download the code from here.
Prerequisites
- Basic knowledge of React, Redus, and Web API
- Visual Studio
- Visual Studio Code
- SQL Server Management Studio
- Node.js
- Bootstrap
This article covers the following,
- Create React Project
- Basic of Redux
- Environment setup for Redux
- Create a Web API Project
- Install Bootstrap and Axios
Redux
Redux is a predictable state container for Javascript applications. We can use Redux with any Javascript framework or library.
According to the Redox official website:
Redux is a predictable state container for JavaScript apps. It helps you write applications that behave consistently, run in different environments (client, server, and native), and are easy to test. On top of that, it provides a great developer experience, such as live code editing combined with a time-traveling debugger. You can use Redux together with React, or with any other view library. It is tiny (2kB, including dependencies), but has a large ecosystem of addons available.
Create a ReactJS project
First, let's create a React application with the following command.
- npx create-react-app reduxapp
Environment setup for Redux
Install Redux
- npm install redux react-redux
Install Redux-Logger
Install Redux-Logger using the following command.
- npm install redux-logger
Install redux-thunk
Install redux-thunk using the following command.
- npm install redux-thunk
Install redux-devtools-extension
Install redux-devtools-extension using the following command.
- npm install redux-devtools-extension
- npm install --save axios
- npm install bootstrap
Now right click on the Src folder and add a new folder named 'redux', and inside this, create a new folder named 'user'.
Inside the user folder, create 6 files named:
- userActions.js
- userReducer.js
- userTypes.js
- index.js
- rootReducer.js
- store.js
File structure of Redux folder:

Now open userTypes.js file and add following code:
- export const GET_USERS_REQUEST = 'GET_USERS_REQUEST'
- export const GET_USERS_SUCCESS = 'GET_USERS_SUCCESS'
- export const GET_USERS_FAILURE = 'GET_USERS_FAILURE'
- import axios from 'axios'
- import {
- GET_USERS_REQUEST,
- GET_USERS_SUCCESS,
- GET_USERS_FAILURE
- } from './userTypes'
- export const GethUsers = () => {
- return (dispatch) => {
- dispatch(getUsersRequest())
- axios
- .get('https://localhost:44317/Api/Student/Getdetaisl')
- .then(response => {
- const users = response.data
- console.log(response.data);
- dispatch(getUsersSuccess(users))
- })
- .catch(error => {
- dispatch(getUsersFailure(error.message))
- })
- }
- }
- export const getUsersRequest = () => {
- return {
- type: GET_USERS_REQUEST
- }
- }
- export const getUsersSuccess = users => {
- return {
- type: GET_USERS_SUCCESS,
- payload: users
- }
- }
- export const getUsersFailure = error => {
- return {
- type: GET_USERS_FAILURE,
- payload: error
- }
- }
- import {
- GET_USERS_REQUEST,
- GET_USERS_SUCCESS,
- GET_USERS_FAILURE
- } from './userTypes'
- const initialState = {
- loading: false,
- users: [],
- error: ''
- }
- const reducer = (state = initialState, action) => {
- switch (action.type) {
- case GET_USERS_REQUEST:
- return {
- ...state,
- loading: true
- }
- case GET_USERS_SUCCESS:
- return {
- loading: false,
- users: action.payload,
- error: ''
- }
- case GET_USERS_FAILURE:
- return {
- loading: false,
- users: [],
- error: action.payload
- }
- default: return state
- }
- }
- export default reducer
- import { createStore, applyMiddleware } from 'redux'
- import { composeWithDevTools } from 'redux-devtools-extension'
- import logger from 'redux-logger'
- import thunk from 'redux-thunk'
- import rootReducer from './rootReducer'
- const store = createStore(
- rootReducer,
- composeWithDevTools(applyMiddleware(logger, thunk))
- )
- export default store










Join the conversation! Your thoughts help the community grow.