ReactJS – Using Newly Created Components


ReactJS – Use Component



”;


A React component represents a small chunk of user interface in a webpage. The primary job of a React component is to render its user interface and update it whenever its internal state is changed. In addition to rendering the UI, it manages the events belongs to its user interface. To summarize, React component provides below functionalities.

Using Components in ReactJS

In this chapter, let us use newly created components and enhance our ExpenseEntryItem component.

Step 1 − Open our expense-manager application in your favorite editor and open the ExpenseEntryItem.js file.

Then, import FormattedMoney and FormattedDate using the following statements.


import FormattedMoney from ''./FormattedMoney'' 
import FormattedDate from ''./FormattedDate''

Step 2 − Next, update the render method by including FormattedMoney and FormattedDater component.


render() {
   return (
      <div>
         <div><b>Item:</b> <em>{this.props.item.name}</em></div>
         <div><b>Amount:</b> 
            <em>
               <FormattedMoney value={this.props.item.amount} />
            </em>
         </div>
         <div><b>Spend Date:</b> 
            <em>
               <FormattedDate value={this.props.item.spendDate} />
            </em>
         </div>
         <div><b>Category:</b> 
            <em>{this.props.item.category}</em></div>
      </div>
   );
}

Here, we have passed the amount and spendDate through value attribute of the components.

The final updated source code of the ExprenseEntryItem component is given below −


import React from ''react''
import FormattedMoney from ''./FormattedMoney''
import FormattedDate from ''./FormattedDate''

class ExpenseEntryItem extends React.Component {
   constructor(props) {
      super(props);
   }
   render() {
      return (
         <div>
            <div><b>Item:</b> <em>{this.props.item.name}</em></div>
            <div><b>Amount:</b> 
               <em>
                  <FormattedMoney value={this.props.item.amount} />
               </em>
            </div>
            <div><b>Spend Date:</b> 
               <em>
                  <FormattedDate value={this.props.item.spendDate} />
               </em>
            </div>
            <div><b>Category:</b> 
               <em>{this.props.item.category}</em></div>
         </div>
      );
   }
}
export default ExpenseEntryItem;

index.js

Open index.js and call the ExpenseEntryItem component by passing the item object.


const item = {
   id: 1, 
   name : "Grape Juice", 
   amount : 30.5, 
   spendDate: new Date("2020-10-10"), 
   category: "Food" 
}
ReactDOM.render(
   <React.StrictMode>
   <ExpenseEntryItem item={item} />
   </React.StrictMode>,
   document.getElementById(''root'')
);

Next, serve the application using npm command.


npm start

Open the browser and enter http://localhost:3000 in the address bar and press enter.


Grape Modules

Advertisements

”;

Leave a Reply

Your email address will not be published. Required fields are marked *