- ReactJS Tutorial
- ReactJS - Home
- ReactJS - Introduction
- ReactJS - Installation
- ReactJS - Features
- ReactJS - Advantages & Disadvantages
- ReactJS - Architecture
- ReactJS - Creating a React Application
- ReactJS - JSX
- ReactJS - Components
- ReactJS - Nested Components
- ReactJS - Using Newly Created Components
- ReactJS - Component Collection
- ReactJS - Styling
- ReactJS - Properties (props)
- ReactJS - Creating Components using Properties
- ReactJS - props Validation
- ReactJS - Constructor
- ReactJS - Component Life Cycle
- ReactJS - Event management
- ReactJS - Creating an Event−Aware Component
- ReactJS - Introduce Events in Expense Manager APP
- ReactJS - State Management
- ReactJS - State Management API
- ReactJS - Stateless Component
- ReactJS - State Management Using React Hooks
- ReactJS - Component Life Cycle Using React Hooks
- ReactJS - Layout Component
- ReactJS - Pagination
- ReactJS - Material UI
- ReactJS - Http client programming
- ReactJS - Form Programming
- ReactJS - Controlled Component
- ReactJS - Uncontrolled Component
- ReactJS - Formik
- ReactJS - Conditional Rendering
- ReactJS - Lists
- ReactJS - Keys
- ReactJS - Routing
- ReactJS - Redux
- ReactJS - Animation
- ReactJS - Bootstrap
- ReactJS - Map
- ReactJS - Table
- ReactJS - Managing State Using Flux
- ReactJS - Testing
- ReactJS - CLI Commands
- ReactJS - Building and Deployment
- ReactJS - Example
- Hooks
- ReactJS - Introduction to Hooks
- ReactJS - Using useState
- ReactJS - Using useEffect
- ReactJS - Using useContext
- ReactJS - Using useRef
- ReactJS - Using useReducer
- ReactJS - Using useCallback
- ReactJS - Using useMemo
- ReactJS - Custom Hooks
- ReactJS Advanced
- ReactJS - Accessibility
- ReactJS - Code Splitting
- ReactJS - Context
- ReactJS - Error Boundaries
- ReactJS - Forwarding Refs
- ReactJS - Fragments
- ReactJS - Higher Order Components
- ReactJS - Integrating With Other Libraries
- ReactJS - Optimizing Performance
- ReactJS - Profiler API
- ReactJS - Portals
- ReactJS - React Without ES6 ECMAScript
- ReactJS - React Without JSX
- ReactJS - Reconciliation
- ReactJS - Refs and the DOM
- ReactJS - Render Props
- ReactJS - Static Type Checking
- ReactJS - Strict Mode
- ReactJS - Web Components
- Additional Concepts
- ReactJS - Date Picker
- ReactJS - Helmet
- ReactJS - Inline Style
- ReactJS - PropTypes
- ReactJS - BrowserRouter
- ReactJS - DOM
- ReactJS - Carousel
- ReactJS - Icons
- ReactJS - Form Components
- ReactJS - Reference API
- ReactJS Useful Resources
- ReactJS - Quick Guide
- ReactJS - Useful Resources
- ReactJS - Discussion
ReactJS - List Expenses
Open ExpenseEntryItemList.js and import connect from redux library.
import { connect } from 'react-redux';
Next, import addExpenseList and deleteExpense actions.
import { getExpenseList, deleteExpense } from '../actions/expenseActions';
Next, add constructor with props.
constructor(props) { super(props); }
Next, call getExpenseList in componentDidMount() life cycle.
componentDidMount() { this.props.getExpenseList(); }
Next, write a method to handle the remove expense option.
handleDelete = (id,e) => { e.preventDefault(); this.props.deleteExpense(id); }
Now, let us write a function, getTotal to calculate the total expenses.
getTotal() { let total = 0; if (this.props.expenses != null) { for (var i = 0; i < this.props.expenses.length; i++) { total += this.props.expenses[i].amount } } return total; }
Next, update the render method and list the expense items.
render() { let lists = []; if (this.props.expenses != null) { lists = this.props.expenses.map((item) => <tr key={item.id}> <td>{item.name}</td> <td>{item.amount}</td> <td>{new Date(item.spendDate).toDateString()}</td> <td>{item.category}</td> <td><a href="#" onClick={(e) => this.handleDelete(item.id, e)}>Remove</a></td> </tr> ); } return ( <div> <table> <thead> <tr> <th>Item</th> <th>Amount</th> <th>Date</th> <th>Category</th> <th>Remove</th> </tr> </thead> <tbody> {lists} <tr> <td colSpan="1" style={{ textAlign: "right" }}>Total Amount</td> <td colSpan="4" style={{ textAlign: "left" }}> {this.getTotal()} </td> </tr> </tbody> </table> </div> ); }
Next, write mapStateToProps and mapDispatchToProps methods.
const mapStateToProps = state => { return { expenses: state.data }; }; const mapDispatchToProps = { getExpenseList, deleteExpense };
Here, we have mapped the expenses item from redux store to expenses property and attach distpatcher, getExpenseList and deleteExpense to component properties.
Finally, connect component to Redux store using connect api.
export default connect( mapStateToProps, mapDispatchToProps )(ExpenseEntryItemList);
The complete source code of the application is given below −
import React from "react"; import { connect } from 'react-redux'; import { getExpenseList, deleteExpense } from '../actions/expenseActions'; class ExpenseEntryItemList extends React.Component { constructor(props) { super(props); } componentDidMount() { this.props.getExpenseList(); } handleDelete = (id, e) => { e.preventDefault(); this.props.deleteExpense(id); } getTotal() { let total = 0; if (this.props.expenses != null) { for (var i = 0; i < this.props.expenses.length; i++) { total += this.props.expenses[i].amount } } return total; } render() { let lists = []; if (this.props.expenses != null) { lists = this.props.expenses.map((item) => <tr key={item.id}> <td>{item.name}</td> <td>{item.amount}</td> <td>{new Date(item.spendDate).toDateString()}</td> <td>{item.category}</td> <td><a href="#" onClick={(e) => this.handleDelete(item.id, e)}>Remove</a> </td> </tr> ); } return ( <div> <table> <thead> <tr> <th>Item</th> <th>Amount</th> <th>Date</th> <th>Category</th> <th>Remove</th> </tr> </thead> <tbody> {lists} <tr> <td colSpan="1" style={{ textAlign: "right" }}>Total Amount</td> <td colSpan="4" style={{ textAlign: "left" }}> {this.getTotal()} </td> </tr> </tbody> </table> </div> ); } } const mapStateToProps = state => { return { expenses: state.data }; }; const mapDispatchToProps = { getExpenseList, deleteExpense }; export default connect( mapStateToProps, mapDispatchToProps )(ExpenseEntryItemList);
Next, serve the application using npm command.
npm start
Next, open the browser and enter http://localhost:3000 in the address bar and press enter.
To Continue Learning Please Login
Login with Google