![](https://vitalflux.com/wp-content/uploads/2015/01/reactjs_calculator_code_example-300x143.png)
- 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
ReactJS Calculator - Code Example
- ROC Curve & AUC Explained with Python Examples - August 28, 2024
- Accuracy, Precision, Recall & F1-Score – Python Examples - August 28, 2024
- Logistic Regression in Machine Learning: Python Example - August 26, 2024
Leave a Reply