We now have a youtube channel. Subscribe!

ReactJS | Introduce Events in Expense Manager App

ReactJS | Introduce Events in Expense Manager App


Hello folks! welcome back to a new section of our tutorial on ReactJS. In this section of our tutorial on ReactJS, we will be studying about how to introduce Events in Expense Manager App.

Let us perform some event management in our React expense application. We can try to highlight the expense entry item in the table when the user moves the cursor over it.

Open expense-manager application in your favorite editor.

Next, open ExpenseEntryItemList.js file and then include a method handleMouseEnter to handle the event fired when the user moves the mouse pointer into the expense items.

handleMouseEnter(e) { 
   e.target.parentNode.classList.add("highlight"); 
}

Here,

  • Event Handler tries to find the parent node (tr) of the event target (td) node via parentNode method. parentNode method is the standard DOM method used to find the immediate parent of the current node.
  • As soon as the parent node is found, event handler accesses the list of the CSS class attached to the parent node and then adds highlight class using add method classList is the standard DOM property to get list of class attached to the node and we can use it to add/remove class from a DOM node.
Next, add a method handleMouseLeave to handle the event fired when the user moves out of the expense item.

handleMouseLeave(e) { 
   e.target.parentNode.classList.remove("highlight"); 
}


Here, Event handler removes the "highlight" class from the DOM.

Next, include a method handleMouseOver in order to check where the mouse is currently positioned. It is optional to find the position of the mouse pointer in the DOM.

handleMouseOver(e) { 
   console.log("The mouse is at (" + e.clientX + ", " + e.clientY + ")"); 
}

Bind all event handlers in the constructor of the component.

this.handleMouseEnter = this.handleMouseEnter.bind(); 
this.handleMouseLeave = this.handleMouseLeave.bind(); 
this.handleMouseOver = this.handleMouseOver.bind();

Next, attach all of the event handlers to the corresponding tag in the render method.

render() {
   const lists = this.props.items.map((item) =>
      <tr key={item.id} onMouseEnter={this.handleMouseEnter} onMouseLeave={this.handleMouseLeave}>
         <td>{item.name}</td>
         <td>{item.amount}</td>
         <td>{new Date(item.spendDate).toDateString()}</td>
         <td>{item.category}</td>
      </tr>
   );
   return (
      <table onMouseOver={this.handleMouseOver}>
         <thead>
            <tr>
               <th>Item</th>
               <th>Amount</th>
               <th>Date</th>
               <th>Category</th>
            </tr>
         </thead>
         <tbody>
            {lists}
         </tbody>
      </table>
   );
}

The final and complete source code of the ExpenseEntryItemList is as follows -

import React from 'react';
import './ExpenseEntryItemList.css';

class ExpenseEntryItemList extends React.Component {
   constructor(props) {
      super(props);

      this.handleMouseEnter = this.handleMouseEnter.bind();
      this.handleMouseLeave = this.handleMouseLeave.bind();
      this.handleMouseOver = this.handleMouseOver.bind();
   }
   handleMouseEnter(e) {
      e.target.parentNode.classList.add("highlight");
   }
   handleMouseLeave(e) {
      e.target.parentNode.classList.remove("highlight");
   }
   handleMouseOver(e) {
      console.log("The mouse is at (" + e.clientX + ", " + e.clientY + ")");
   }
   render() {
      const lists = this.props.items.map((item) =>
         <tr key={item.id} onMouseEnter={this.handleMouseEnter} onMouseLeave={this.handleMouseLeave}>
            <td>{item.name}</td>
            <td>{item.amount}</td>
            <td>{new Date(item.spendDate).toDateString()}</td>
            <td>{item.category}</td>
         </tr>
      );
      return (
         <table onMouseOver={this.handleMouseOver}>
            <thead>
               <tr>
                  <th>Item</th>
                  <th>Amount</th>
                  <th>Date</th>
                  <th>Category</th>
               </tr>
            </thead>
            <tbody>
               {lists}
            </tbody>
         </table>
      );
   }
}
export default ExpenseEntryItemList;


Open the css file, ExpenseEntryItemList.css and add a css class, highlight.

tr.highlight td { 
   background-color: #a6a8bd; 
}

Next, open the index.js file and make use of the ExpenseEntryItemList component.

import React from 'react';
import ReactDOM from 'react-dom';
import ExpenseEntryItemList from './components/ExpenseEntryItemList'

const items = [
   { id: 1, name: "Pizza", amount: 80, spendDate: "2020-10-10", category: "Food" },
   { id: 2, name: "Grape Juice", amount: 30, spendDate: "2020-10-12", category: "Food" },
   { id: 3, name: "Cinema", amount: 210, spendDate: "2020-10-16", category: "Entertainment" },
   { id: 4, name: "Java Programming book", amount: 242, spendDate: "2020-10-15", category: "Academic" },
   { id: 5, name: "Mango Juice", amount: 35, spendDate: "2020-10-16", category: "Food" },
   { id: 6, name: "Dress", amount: 2000, spendDate: "2020-10-25", category: "Cloth" },
   { id: 7, name: "Tour", amount: 2555, spendDate: "2020-10-29", category: "Entertainment" },
   { id: 8, name: "Meals", amount: 300, spendDate: "2020-10-30", category: "Food" },
   { id: 9, name: "Mobile", amount: 3500, spendDate: "2020-11-02", category: "Gadgets" },
   { id: 10, name: "Exam Fees", amount: 1245, spendDate: "2020-11-04", category: "Academic" }
]
ReactDOM.render(
   <React.StrictMode>
      <ExpenseEntryItemList items={items} />
   </React.StrictMode>,
   document.getElementById('root')
);

Next, serve the application making use of npm command.

npm start

Next, open the web browser and then enter http://localhost:3000 in the address bar and press enter.

The application will respond to the mouse events and highlight the currently selected row.

ItemAmountDateCategory
Pizza80Sat Oct 10 2020Food
Grape Juice30Man Oct 12 2020Food
Cinema210Fri Oct 16 2020Entertainment
Java Programming book242Thu Oct 15 2020Academic
Mango Juice35Fri Oct 16 2020Food
Dress2000Sun Oct 25 2020Cloth
Tour2555Thu Oct 29 2020Entertainment
Meals300Fri Oct 30 2020Food
Mobile3500Mon Nov 02 2020Gadgets
Exam Fees1245Wed Nov 04 2020Academic


Alright guys! This is where we are going to be rounding up for this tutorial. In our next tutorial, we will be studying about ReactJS State Management.

Feel free to ask your questions where necessary and we will attend to them as soon as possible. If this tutorial was helpful to you, you can use the share button to share this tutorial.

Follow us on our various social media platforms to stay updated with our latest tutorials. You can also subscribe to our newsletter in order to get our tutorials delivered directly to your emails.

Thanks for reading and bye for now.

Post a Comment

Hello dear readers! Please kindly try your best to make sure your comments comply with our comment policy guidelines. You can visit our comment policy page to view these guidelines which are clearly stated. Thank you.
© 2022 ‧ WebDesignTutorialz. All rights reserved. Developed by Jago Desain