Categories: JavascriptWeb

ReactJS – Component-oriented UI Design Explained with Calculator Example

This article represents high level concepts with code examples used to create a trivial calculator using ReactJS. Please feel free to comment/suggest if I missed to mention one or more important points. Also, sorry for the typos.
Following are the key points described later in this article:
  • Design & Analysis – ReactJS Calculator
  • Code Sample – ReactJS Calculator

 

Design & Analysis – ReactJS Calculator

Calculator consists of three components, one (Operation) of which is reusable component. Following are these components:

  • InputField: This represents textfield which allows uses to enter the number
  • Operation: This component represents operation such as addtion (+), subtraction (-), multiplication (*), division (/) in the example below. The component has two attributes such as label (which defines +,-,*,/) and ops that represents actual operation of addition, subtraction, multiplication and division. The same component is used for different operation.
  • Output: This represents the output of calculation.

Following are some of the key points to be noted:

  • Each component can be defined using following code:
     var SomeClassName = React.createClass({
         render: function() {
          return (
            

    {this.props.someAttribute}

    ); } });

    This component could be represented in HTML syntax using following:

       
      
  • The value that needs to be updated on UI is represented using “state”. To update UI, this.setState API is called.
  • The event in the children component is passed to the parent component. This is demonstrated in following code example:
     var InputField = React.createClass({
      handleChange: function() {
       this.props.onNumberChange({value: this.refs.inputno.getDOMNode().value})   
      },
      handleOnFocus: function() {
       this.refs.inputno.getDOMNode().value = '';
      },
      render: function() {
       return(
        
        );
      }
     });
    

    In above code sample, onNumberChange is a custom event handler method of InputField component. This method is passed value such as “this.refs.inputno.getDOMNode().value” as shown in the code sample. This value is passed to the parent method, handleNumberChange owing to the way the component is defined. Take a look at the code below.

       
      
  • CSS class is specified using the attribute className.
  • Local style is applied using style={{}} syntax.

 

Code Sample – ReactJS Calculator


  
    Hello React
    
    
    
  
  
   
); } }); // InputField component which has following three methods: // handleChange: Used to capture onChange event in textfield // handleOnFocus: Used to capture onFocus event // render: Code to render the component // var InputField = React.createClass({ handleChange: function() { this.props.onNumberChange({value: this.refs.inputno.getDOMNode().value}) }, handleOnFocus: function() { this.refs.inputno.getDOMNode().value = ''; }, render: function() { return( ); } }); // Operation component which has following two methods: // handleOperation: Used to capture onClick event // render: Code to render the component // var Operation = React.createClass({ handleOperation: function(type) { this.props.ops(); }, render: function() { return( ); } }); var Output = React.createClass({ render: function() { return (

{this.props.result}

); } }); React.render( , document.getElementById('calculator') );

 

Ajitesh Kumar

I have been recently working in the area of Data analytics including Data Science and Machine Learning / Deep Learning. I am also passionate about different technologies including programming languages such as Java/JEE, Javascript, Python, R, Julia, etc, and technologies such as Blockchain, mobile computing, cloud-native technologies, application security, cloud computing platforms, big data, etc. I would love to connect with you on Linkedin. Check out my latest book titled as First Principles Thinking: Building winning products using first principles thinking.

Share
Published by
Ajitesh Kumar

Recent Posts

Retrieval Augmented Generation (RAG) & LLM: Examples

Last updated: 25th Jan, 2025 Have you ever wondered how to seamlessly integrate the vast…

6 days ago

How to Setup MEAN App with LangChain.js

Hey there! As I venture into building agentic MEAN apps with LangChain.js, I wanted to…

2 weeks ago

Build AI Chatbots for SAAS Using LLMs, RAG, Multi-Agent Frameworks

Software-as-a-Service (SaaS) providers have long relied on traditional chatbot solutions like AWS Lex and Google…

2 weeks ago

Creating a RAG Application Using LangGraph: Example Code

Retrieval-Augmented Generation (RAG) is an innovative generative AI method that combines retrieval-based search with large…

3 weeks ago

Building a RAG Application with LangChain: Example Code

The combination of Retrieval-Augmented Generation (RAG) and powerful language models enables the development of sophisticated…

3 weeks ago

Building an OpenAI Chatbot with LangChain

Have you ever wondered how to use OpenAI APIs to create custom chatbots? With advancements…

3 weeks ago