Here, in this write-up, we will try to create a sample application in React and will try to integrate Redux into it.

Redux offers the below components.

The following diagram can give you a better understanding.

Integration React With Redux

React and Redux manage unidirectional data flow. Let us create a sample application to understand this concept.

Create A React application first with the following command -

npx create-react-app <app name>

Now, open the application in Visual Studio Code. Remove the existing HTML code from app.js. Then, install the Redux component using this command.

npm install redux react-redux --save

Create a new sample component form in React application.

  1. import React, { Component } from 'react'
  2. import { connect } from 'react-redux'
  3. class Form extends Component {
  4. render(props) {
  5. return (
  6. <div>
  7. <h4>{this.props.name}</h4>
  8. <button>change data</button>
  9. </div>
  10. )
  11. }
  12. }
  13. function mapStateToProps(state) {
  14. return{
  15. name:state.name
  16. }
  17. }
  18. export default connect(mapStateToProps)(Form);

Add the Form component to app component.

  1. import React, { Component } from "react";
  2. import Form from "./form";
  3. import "./App.css";
  4. class App extends Component {
  5. render() {
  6. return (
  7. <div className="App">
  8. <header />
  9. <h1>testing application</h1>
  10. <Form />
  11. <footer />
  12. </div>
  13. );
  14. }
  15. }
  16. export default App;

Open index.js in the src folder and change as per the below code.

  1. import React from 'react'
  2. import { render } from 'react-dom'
  3. import { Provider } from 'react-redux'
  4. import { createStore } from 'redux'
  5. import App from './App'
  6. const initialSate = {
  7. name: "john"
  8. }
  9. const rootReducer = (state = initialSate, action) => {
  10. console.log('rootReducer', action);
  11. return state;
  12. }
  13. const store = createStore(rootReducer)
  14. render(
  15. <Provider store={store}>
  16. <App />
  17. </Provider>,
  18. document.getElementById('root')
  19. )

In the above code, we can initiate Redux into the React application, and get the default data from store to display in a component. Let us try to understand how to update the store on any event or an action.

Add a Form component mapDispatchToProps and call the method on button click. For details, please find the below code.

  1. import React, { Component } from 'react'
  2. import { connect } from 'react-redux'
  3. class Form extends Component {
  4. render(props) {
  5. return (
  6. <div>
  7. <h4>{this.props.name}</h4>
  8. <button onClick={this.props.onAddDataClick}>change data</button> <br />
  9. </div>
  10. )
  11. }
  12. }
  13. function mapStateToProps(state) {
  14. return{
  15. name:state.name
  16. }
  17. }
  18. function mapDispatchToProps(dispatch){
  19. return{
  20. onAddDataClick:()=>{
  21. console.log('onAddDataClick - onButtonClick');
  22. const action={type:'ADD_DATA'};
  23. dispatch(action);
  24. }
  25. }
  26. }
  27. export default connect(mapStateToProps , mapDispatchToProps)(Form);

Also, we need to modify the dispatcher method and add a switch case to get the action. According to that, we can modify the state.

  1. const rootReducer = (state = initialSate, action) => {
  2. console.log('rootReducer', action);
  3. switch(action.type){
  4. case 'ADD_DATA':
  5. return Object.assign({},state,{name: "danny"})
  6. }
  7. return state;
  8. }

One can create separate files for store, reducer, and actions so as to extend the application as per the requirement. This application gives you a simple idea of how to create a React application and integrate Redux.