Integrating React Component with Redux Store

Introduction

 
In the previous series, we learned about the basic concept of Redux and how it works as a standalone. Now in this article, we will be going to learn Redux with React. How we can setup Redux in a React project.
 

Redux setup in React App

 
First, we will create a React application to get started with the React-Redux application by using the command:
  1. npx create-react-app  
 
So, after the execution of the command, the project is successfully executed,
 
 
 
Now after successful creation of the project, we will install 2 important packages
  1. npm install redux react-redux   
 
After successfully adding packages, it will be updated in package.json
 
 
Now in src folder create a new folder named components,
 
 
Now create UserContainer.js file,
 
 
Add code in UserContainer.js,
  1. import React from 'react'  
  2.   
  3. function UserContainer() {  
  4.     return (  
  5.         <div>  
  6.             <input type="text" placeholder="User Name"/>  
  7.             <input type="text" placeholder="Password"/>  
  8.             <button>Login</button>  
  9.         </div>  
  10.     )  
  11. }  
  12.   
  13. export default UserContainer  
Add the UserContainer in App.js,
  1. import React from 'react';  
  2. import './App.css';  
  3. import UserContainer from './components/UserContainer';  
  4.   
  5. function App() {  
  6.   return (  
  7.     <div className="App">  
  8.      <UserContainer/>  
  9.     </div>  
  10.   );  
  11. }  
  12.   
  13. export default App;   
And run the project, the output will be displayed as below,
 
 
Now for the next step, we will be creating a new folder named Redux,
 
 
Now following our folder structure, we will create folders feature-wise. Create a folder named login which will include all actions and constants related to login feature
 
 
Now let’s create an action named as loginActions.js,
  1. const loginUser = () => {  
  2.     return {  
  3.         type: ‘LOGIN’,  
  4.   
  5.     }  
  6. }   
After action is successfully created, we will create type so instead of using string in single quote we can use it as constant, loginTypes.js
  1. export const LOGIN = 'LOGIN'   
After creating Type file , it will be imported in action to use constant directly,
  1. import { LOGIN } from './loginTypes'  
  2. export const loginUser = () => {  
  3.     return {  
  4.         type: LOGIN,  
  5.         userName:'user',  
  6.         password:'user'  
  7.     }  
  8. }   
Now we will create Reducer named loginReducer.js,
  1. import { LOGIN } from './loginTypes'  
  2.   
  3. function callLoginApi(username, password) {  
  4.     if (username === 'admin' && password === 'admin') {  
  5.         return "Login Success";  
  6.     } else {  
  7.         return 'Invalid email and password';  
  8.     }  
  9. }  
  10.   
  11. const initialState = {  
  12.     userName: 'test',  
  13.     password: 'test',  
  14.     loggedInStatus: ''  
  15. }  
  16.   
  17. const loginReducer = (state = initialState, action) => {  
  18.     switch (action.type) {  
  19.         case LOGIN: return {  
  20.             ...state,  
  21.             userName: action.userName,  
  22.             password: action.password,  
  23.             loggedInStatus: callLoginApi(action.userName, action.password)  
  24.         }  
  25.         defaultreturn state  
  26.     }  
  27. }  
  28.   
  29. export default loginReducer   
After creating reducer, we will create store.js file,
  1. import {createStore} from 'redux'  
  2. import loginReducer from './login/loginReducer'  
  3.   
  4. const store = createStore(loginReducer)  
  5.   
  6. export default store   
Now so far, we are done with Action, type, store, and Reducer. Now in the next step, we are using it in our container,
 
First, we will create a constant named mapStateToProp which will map property in the container,
  1. const mapStateToProp = state =>{  
  2.     return{  
  3.         userName:state.userName,  
  4.         password:state.password  
  5.     }  
  6. }   
mapStateToProps – This function itself is suggested by its name. It acts as the connector between the Redux state to the react component. In this way, a connected React component will have access to the required store.
 
Now we will connect our container with the action creator method.
 
So, in a a second step we will create another function named mapDispatchToProps
  1. const mapDispatchToProps = dispatch =>{  
  2.     return{  
  3.         login: ()=> dispatch(login())  
  4.     }  
  5. }   
mapDispatchToProps – This function is the same as mapStateToProps but that is for actions. mapDispatchToProps connects Redux action to the props of the React component. By doing this a connected React component will be able to send data to the store.
 
So now to connect action creator with props and state,
 
I have created an index.js file in which exported action creator method,
  1. export {loginUser} from './login/loginActions'   
Now updating UserContainer.js file,
  1. import { connect } from 'react-redux'  
  2. import { loginUser } from '../redux'  
  1. export default connect(mapStateToProp, mapDispatchToProps)(UserContainer)  
Now the whole UserContainer.js code will be as below,
  1. import React from 'react'  
  2. import { connect } from 'react-redux'  
  3. import { loginUser } from '../redux'  
  4.   
  5. function UserContainer(props) {  
  6.     return (  
  7.         <div>  
  8.             <input type="text" placeholder="User Name" onChange={(e) => { userName = e.target.value }} />  
  9.             <input type="text" placeholder="Password" onChange={(e) => { password = e.target.value }} />  
  10.             <button onClick={props.loginUser}>Login</button>  
  11.             <p>{props.loggedInStatus}</p>  
  12.         </div>  
  13.     )  
  14. }  
  15.   
  16. const mapStateToProp = state => {  
  17.     return {  
  18.         userName: state.userName,  
  19.         password: state.password,  
  20.         loggedInStatus: state.loggedInStatus  
  21.     }  
  22. }  
  23.   
  24. const mapDispatchToProps = dispatch => {  
  25.     return {  
  26.         loginUser: () => dispatch(loginUser())  
  27.     }  
  28. }  
  29.   
  30. export default connect(mapStateToProp, mapDispatchToProps)(UserContainer)   
So, after connecting Redux to React components, we will wrap it in with provider in App.js
  1. import React from 'react';  
  2. import './App.css';  
  3. import { Provider } from 'react-redux'  
  4. import store from './redux/store'  
  5. import UserContainer from './components/UserContainer';  
  6.   
  7. function App() {  
  8.   return (  
  9.     <Provider store={store}>  
  10.       <div className="App">  
  11.         <UserContainer />  
  12.       </div>  
  13.     </Provider>  
  14.   );  
  15. }  
  16.   
  17. export default App;   
Provider acts as a wrapper in React applications and ais ware of the entire Redux store.
 
So in this way the whole Redux-React application works.
 
The output will be as below,
 
 
 
Now update username and password property in loginActions.js
  1. import { LOGIN } from './loginTypes'  
  2. export const loginUser = () => {  
  3.     return {  
  4.         type: LOGIN,  
  5.         userName:'admin',  
  6.         password:'admin'  
  7.     }  
  8. }  
Now the output will be as below,
 
 

Summary

 
In this article, we have learned about connecting the Redux store to the React component. You can download the source code attached along with this article.


Similar Articles